Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get max value index for a list of dicts

I'm trying to get the index of the dictionary with the max 'size' in a list of dictionaries like the following:

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

with the following code I can take the maximum size:

items = [x['size'] for x in ld]
print(max(items))

How can I take its index now? Is there an easy way?

Test:

I just figured I can do this:

items = [x['size'] for x in ld]
max_val = max(items)
print(items.index(max_val))

is this correct?

like image 803
Kvash Avatar asked May 30 '15 13:05

Kvash


2 Answers

Tell max() how to calculate the maximum for a sequence of indices:

max(range(len(ld)), key=lambda index: ld[index]['size'])

This'll return the index for which the size key is the highest:

>>> ld = [{'prop': 'foo', 'size': 100}, {'prop': 'boo', 'size': 200}]
>>> max(range(len(ld)), key=lambda index: ld[index]['size'])
1
>>> ld[1]
{'size': 200, 'prop': 'boo'}

If you wanted that dictionary all along, then you could just use:

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

and to get both the index and the dictionary, you could use enumerate() here:

max(enumerate(ld), key=lambda item: item[1]['size'])

Some more demoing:

>>> max(ld, key=lambda d: d['size'])
{'size': 200, 'prop': 'boo'}
>>> max(enumerate(ld), key=lambda item: item[1]['size'])
(1, {'size': 200, 'prop': 'boo'})

The key function is passed each element in the input sequence in turn, and max() will pick the element where the return value of that key function is highest.

Using a separate list to extract all the size values then mapping that back to your original list is not very efficient (you now need to iterate over the list twice). list.index() cannot work as it has to match the whole dictionary, not just one value in it.

like image 99
Martijn Pieters Avatar answered Oct 11 '22 10:10

Martijn Pieters


You can pass the enumerate(ld) to max function with a proper key :

>>> max(enumerate(ld),key=lambda arg:arg[1]['size'])[0]
1

If you just want the dictionary with max size value, as a Pythonic approach you could use operator.itemgetter function as the key:

In [10]: from operator import itemgetter 

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

In [12]: fn = itemgetter('size')

In [13]: max(ld, key=fn)
Out[13]: {'prop': 'boo', 'size': 200}
like image 42
Mazdak Avatar answered Oct 11 '22 09:10

Mazdak