I have a list of dictionaries that I want to filter.
[{"Slope": -0.562, "Count": 3},
{"Slope": -0.362, "Count": 6},
{"Slope": -0.762, "Count": 8},
{"Slope": -0.562, "Count": 12},
{"Slope": 2.5, "Count": 34},
{"Slope": 1.52, "Count": 2},
{"Slope": .56, "Count": 6}]
My goal is to get a list of two dictionaries. One with the "highest count and a POSITVE slope" and the other with the "highest count and NEGATIVE slope".
My plan was to filter all the positive and negative ones out, then sort each list and then create a new list with the first record of each.
Sorting the list isnt an issue for me, i've got this!
lines_lst.sort(key=lambda i: i['lines_count'])
But filtering doesn't seem to work when I try this as it returns a dictionary.
positive_lines = next(item for item in lines_lst if item["Slope"] > 0)
Does anyone have a solution that ends up with the below?
[{"Slope": -0.562, "Count": 12},{"Slope": 2.5, "Count": 34}]