0

From a list of dictionaries I am trying to return the dictionary or dictionaries with the highest value. This question is similar to this question but slightly different.

Given a list of dictionaries:

ld = [{'prop': 'foo', 'size': 100}, {'prop': 'boo', 'size': 200}, {'prop': 'loo', 'size': 200}]

I would like to return the two dictionaries with the highest score i.e. boo and loo.

If I run the code shown in the question mentioned above:

max(ld, key=lambda d: d['size'])

I only get the first highest dictionary returned.

Thanks in advance and let me know if you need any clarity.

Community
  • 1
  • 1
sammy88888888
  • 458
  • 1
  • 5
  • 18

1 Answers1

0

I am afraid there is no simple one-liner for this in O(n) complexity. If the performance is not an issue for you, I propose using sort:

ld = [{'prop': 'foo', 'size': 100}, {'prop': 'boo', 'size': 300}, {'prop': 'loo', 'size': 700}, {'prop': 'loo', 'size': 200}]

print(sorted(range(len(ld)), key=lambda index: ld[index]['size'])[-2:])
indices = sorted(range(len(ld)), key=lambda index: ld[index]['size'])[-2:]
for index in indices:
    print(index)
    print(ld[index])

The results:

[1, 2]                                                                                                                                                                                                                            
1                                                                                                                                                                                                                                 
{'size': 300, 'prop': 'boo'}                                                                                                                                                                                                      
2                                                                                                                                                                                                                                 
{'size': 700, 'prop': 'loo'}  

If you need linear complexity, I think you have to write your own code, please consult Get the second largest number in a list in linear time

Please note the answer is in Python 3.

If flexibility in your O(n) solution is needed (i.e. which element of dictionary decides about order), you can use something like that:

def myfun(mylist, getter):
    for item in mylist:
        print(getter(item))

myfun(ld, lambda item: item['size'])

So in this simple example we use a function (created by lambda) getter to choose an element from a dictionary. In your code, it will be used to get a second and first maximum element.

Community
  • 1
  • 1
bartoszukm
  • 693
  • 3
  • 10
  • Thanks for the response. I may have phrased the question ambiguously but I'm looking to return the two (or more) dictionaries where the **size** contained within each dictionary is **equal**. I'm also working in python 2.7. Cheers. – sammy88888888 Jan 31 '16 at 19:06