2202

I have a list of dictionaries and want each item to be sorted by a specific value.

Take into consideration the list:

[{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}]

When sorted by name, it should become:

[{'name':'Bart', 'age':10}, {'name':'Homer', 'age':39}]
yatu
  • 75,195
  • 11
  • 47
  • 89
masi
  • 118
  • 3
  • 5
  • 9
  • 3
    Reading the answer and looking on [operator.itemgetter](https://docs.python.org/3/library/operator.html#operator.itemgetter). Can I sort on multiple value in the same process (for example we have `[{'name':'Bart', 'age':10, 'note':3},{'name':'Homer','age':10,'note':2},{'name':'Vasile','age':20,'note':3}]` And to use: `from operator import itemgetter newlist = sorted(old_list, key=itemgetter(-'note','name')` EDIT: Tested, and it is working but I don't know how to make note DESC and name ASC. – Claudiu May 21 '20 at 07:13

17 Answers17

2849

The sorted() function takes a key= parameter

newlist = sorted(list_to_be_sorted, key=lambda k: k['name']) 

Alternatively, you can use operator.itemgetter instead of defining the function yourself

from operator import itemgetter
newlist = sorted(list_to_be_sorted, key=itemgetter('name')) 

For completeness, add reverse=True to sort in descending order

newlist = sorted(l, key=itemgetter('name'), reverse=True)
Boris
  • 7,044
  • 6
  • 62
  • 63
Mario F
  • 36,999
  • 5
  • 32
  • 36
  • 43
    Using key is not only cleaner but more effecient too. – jfs Sep 16 '08 at 15:03
  • 5
    The fastest way would be to add a newlist.reverse() statement. Otherwise you can define a comparison like cmp=lambda x,y: - cmp(x['name'],y['name']). – Mario F Oct 13 '09 at 07:14
  • 3
    if the sort value is a number you could say: lambda k: (k['age'] * -1) to get a reverse sort – Philluminati Nov 20 '09 at 15:16
  • 3
    This also applies to a list of tuples, if you use `itemgetter(i)` where `i` is the index of the tuple element to sort on. – radicand Jul 11 '12 at 23:14
  • 47
    `itemgetter` accepts more than one argument: `itemgetter(1,2,3)` is a function that return a tuple like `obj[1], obj[2], obj[3]`, so you can use it to do complex sorts. – Bakuriu Sep 07 '12 at 17:59
  • in case of unicode strings `import locale / locale.setlocale(locale.LC_ALL, "") / newlist = sorted(list_to_be_sorted, key=lambda k: locale.strxfrm(k['name']))` works inspired from https://stackoverflow.com/questions/1097908/how-do-i-sort-unicode-strings-alphabetically-in-python – webenformasyon Oct 15 '19 at 09:13
  • what if we don't know the value of key? – sattva_venu Jan 28 '21 at 12:53
184
import operator

To sort the list of dictionaries by key='name':

list_of_dicts.sort(key=operator.itemgetter('name'))

To sort the list of dictionaries by key='age':

list_of_dicts.sort(key=operator.itemgetter('age'))
cedbeu
  • 1,651
  • 12
  • 22
  • 11
    Anyway to combine name and age ? (like in SQL ORDER BY name,age ?) – monojohnny Feb 17 '10 at 13:10
  • 35
    @monojohnny: yes, just have the key return a tuple, `key=lambda k: (k['name'], k['age'])`. (or `key=itemgetter('name', 'age')`). tuple's `cmp` will compare each element in turn. it's bloody brilliant. – Claudiu Sep 04 '13 at 22:21
  • 1
    In the documentation (http://docs.python.org/2/tutorial/datastructures.html) the optional `key` argument for `list.sort()` is not described. Any idea where to find that? – TTT Feb 21 '14 at 15:21
  • 2
    @TTT: See the [library documentation](https://docs.python.org/2/library/stdtypes.html#mutable-sequence-types) for `list` and friends. – Kevin Feb 19 '15 at 14:56
79
my_list = [{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}]

my_list.sort(lambda x,y : cmp(x['name'], y['name']))

my_list will now be what you want.

Or better:

Since Python 2.4, there's a key argument is both more efficient and neater:

my_list = sorted(my_list, key=lambda k: k['name'])

...the lambda is, IMO, easier to understand than operator.itemgetter, but your mileage may vary.

pjz
  • 38,171
  • 5
  • 45
  • 60
  • what could be done if the key is unknown and keeps changing?I mean list of dicts with just one key and value but the key and value could not be defined as they keep changing. – Sam Dec 01 '20 at 14:51
  • I'd need more of an example to look at. Try submitting a possible solution on the codereview stackexchange and asking if there's a better way. – pjz Dec 30 '20 at 01:02
  • @Sam if you want to sort by the value of the single key in the dict, even if you don't know the key, you can do `key=lambda k: list(k.values())[0]` – pjz Mar 10 '21 at 06:38
57

If you want to sort the list by multiple keys, you can do the following:

my_list = [{'name':'Homer', 'age':39}, {'name':'Milhouse', 'age':10}, {'name':'Bart', 'age':10} ]
sortedlist = sorted(my_list , key=lambda elem: "%02d %s" % (elem['age'], elem['name']))

It is rather hackish, since it relies on converting the values into a single string representation for comparison, but it works as expected for numbers including negative ones (although you will need to format your string appropriately with zero paddings if you are using numbers).

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Dologan
  • 3,880
  • 2
  • 28
  • 30
  • 3
    sorted using timsort which is stable, you can call sorted several times to have a sort on several criteria – njzk2 May 29 '13 at 13:41
  • 1
    njzk2's comment wasn't immediately clear to me so I found the following. You can just sort twice as njzk2 suggests, or pass multiple arguments to operator.itemgetter in the top answer. Link: http://stackoverflow.com/questions/5212870/sorting-a-python-list-by-two-criteria – Permafacture Aug 23 '13 at 21:05
  • 17
    No need to convert to string. Just return a tuple as the key. – Winston Ewert Dec 15 '13 at 04:55
  • Sorting multiple times is the easiest generic solution without hacks: http://stackoverflow.com/a/29849371/1805397 – wouter bolsterlee Apr 24 '15 at 13:59
33
a = [{'name':'Homer', 'age':39}, ...]

# This changes the list a
a.sort(key=lambda k : k['name'])

# This returns a new list (a is not modified)
sorted(a, key=lambda k : k['name']) 
forzagreen
  • 1,776
  • 25
  • 30
31
import operator
a_list_of_dicts.sort(key=operator.itemgetter('name'))

'key' is used to sort by an arbitrary value and 'itemgetter' sets that value to each item's 'name' attribute.

efotinis
  • 13,400
  • 5
  • 28
  • 36
23

I guess you've meant:

[{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}]

This would be sorted like this:

sorted(l,cmp=lambda x,y: cmp(x['name'],y['name']))
Bartosz Radaczyński
  • 17,696
  • 14
  • 48
  • 58
21

Using the Schwartzian transform from Perl,

py = [{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}]

do

sort_on = "name"
decorated = [(dict_[sort_on], dict_) for dict_ in py]
decorated.sort()
result = [dict_ for (key, dict_) in decorated]

gives

>>> result
[{'age': 10, 'name': 'Bart'}, {'age': 39, 'name': 'Homer'}]

More on the Perl Schwartzian transform:

In computer science, the Schwartzian transform is a Perl programming idiom used to improve the efficiency of sorting a list of items. This idiom is appropriate for comparison-based sorting when the ordering is actually based on the ordering of a certain property (the key) of the elements, where computing that property is an intensive operation that should be performed a minimal number of times. The Schwartzian Transform is notable in that it does not use named temporary arrays.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
kiriloff
  • 22,522
  • 32
  • 127
  • 207
  • 10
    Python has supported the `key=` for `.sort` since 2.4, that is year 2004, it does the Schwartzian transform within the sorting code, in C; thus this method is useful only on Pythons 2.0-2.3. all of which are more than 12 years old. – Antti Haapala Feb 15 '15 at 20:11
21

You could use a custom comparison function, or you could pass in a function that calculates a custom sort key. That's usually more efficient as the key is only calculated once per item, while the comparison function would be called many more times.

You could do it this way:

def mykey(adict): return adict['name']
x = [{'name': 'Homer', 'age': 39}, {'name': 'Bart', 'age':10}]
sorted(x, key=mykey)

But the standard library contains a generic routine for getting items of arbitrary objects: itemgetter. So try this instead:

from operator import itemgetter
x = [{'name': 'Homer', 'age': 39}, {'name': 'Bart', 'age':10}]
sorted(x, key=itemgetter('name'))
Owen
  • 751
  • 4
  • 6
19

You have to implement your own comparison function that will compare the dictionaries by values of name keys. See Sorting Mini-HOW TO from PythonInfo Wiki

Matej
  • 5,122
  • 1
  • 25
  • 25
15

Sometimes we need to use lower(). For example,

lists = [{'name':'Homer', 'age':39},
  {'name':'Bart', 'age':10},
  {'name':'abby', 'age':9}]

lists = sorted(lists, key=lambda k: k['name'])
print(lists)
# [{'name':'Bart', 'age':10}, {'name':'Homer', 'age':39}, {'name':'abby', 'age':9}]

lists = sorted(lists, key=lambda k: k['name'].lower())
print(lists)
# [ {'name':'abby', 'age':9}, {'name':'Bart', 'age':10}, {'name':'Homer', 'age':39}]
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
uingtea
  • 3,739
  • 2
  • 18
  • 32
12

Here is the alternative general solution - it sorts elements of a dict by keys and values.

The advantage of it - no need to specify keys, and it would still work if some keys are missing in some of dictionaries.

def sort_key_func(item):
    """ Helper function used to sort list of dicts

    :param item: dict
    :return: sorted list of tuples (k, v)
    """
    pairs = []
    for k, v in item.items():
        pairs.append((k, v))
    return sorted(pairs)
sorted(A, key=sort_key_func)
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
vvladymyrov
  • 5,177
  • 1
  • 28
  • 45
12

Using the Pandas package is another method, though its runtime at large scale is much slower than the more traditional methods proposed by others:

import pandas as pd

listOfDicts = [{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}]
df = pd.DataFrame(listOfDicts)
df = df.sort_values('name')
sorted_listOfDicts = df.T.to_dict().values()

Here are some benchmark values for a tiny list and a large (100k+) list of dicts:

setup_large = "listOfDicts = [];\
[listOfDicts.extend(({'name':'Homer', 'age':39}, {'name':'Bart', 'age':10})) for _ in range(50000)];\
from operator import itemgetter;import pandas as pd;\
df = pd.DataFrame(listOfDicts);"

setup_small = "listOfDicts = [];\
listOfDicts.extend(({'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}));\
from operator import itemgetter;import pandas as pd;\
df = pd.DataFrame(listOfDicts);"

method1 = "newlist = sorted(listOfDicts, key=lambda k: k['name'])"
method2 = "newlist = sorted(listOfDicts, key=itemgetter('name')) "
method3 = "df = df.sort_values('name');\
sorted_listOfDicts = df.T.to_dict().values()"

import timeit
t = timeit.Timer(method1, setup_small)
print('Small Method LC: ' + str(t.timeit(100)))
t = timeit.Timer(method2, setup_small)
print('Small Method LC2: ' + str(t.timeit(100)))
t = timeit.Timer(method3, setup_small)
print('Small Method Pandas: ' + str(t.timeit(100)))

t = timeit.Timer(method1, setup_large)
print('Large Method LC: ' + str(t.timeit(100)))
t = timeit.Timer(method2, setup_large)
print('Large Method LC2: ' + str(t.timeit(100)))
t = timeit.Timer(method3, setup_large)
print('Large Method Pandas: ' + str(t.timeit(1)))

#Small Method LC: 0.000163078308105
#Small Method LC2: 0.000134944915771
#Small Method Pandas: 0.0712950229645
#Large Method LC: 0.0321750640869
#Large Method LC2: 0.0206089019775
#Large Method Pandas: 5.81405615807
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
abby sobh
  • 1,464
  • 18
  • 15
  • 4
    I ran your code and found a mistake in the the timeit.Timer args for Large Method Pandas: you specify "setup_small" where it should be "setup_large". Changing that arg caused the program to run without finishing, and I stopped it after more than 5 minutes. When I ran it with "timeit(1)", the Large Method Pandas finished in 7.3 sec, much worse than LC or LC2. – clp2 Nov 07 '16 at 04:05
  • You're quite right, that was quite an oversight on my part. I no longer recommend it for large cases! I have edited the answer to simply allow it as a possibility, the use case is still up for debate. – abby sobh Nov 08 '16 at 22:58
8

Let's say I have a dictionary D with the elements below. To sort, just use the key argument in sorted to pass a custom function as below:

D = {'eggs': 3, 'ham': 1, 'spam': 2}
def get_count(tuple):
    return tuple[1]

sorted(D.items(), key = get_count, reverse=True)
# Or
sorted(D.items(), key = lambda x: x[1], reverse=True)  # Avoiding get_count function call

Check this out.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
8

If you do not need the original list of dictionaries, you could modify it in-place with sort() method using a custom key function.

Key function:

def get_name(d):
    """ Return the value of a key in a dictionary. """

    return d["name"]

The list to be sorted:

data_one = [{'name': 'Homer', 'age': 39}, {'name': 'Bart', 'age': 10}]

Sorting it in-place:

data_one.sort(key=get_name)

If you need the original list, call the sorted() function passing it the list and the key function, then assign the returned sorted list to a new variable:

data_two = [{'name': 'Homer', 'age': 39}, {'name': 'Bart', 'age': 10}]
new_data = sorted(data_two, key=get_name)

Printing data_one and new_data.

>>> print(data_one)
[{'name': 'Bart', 'age': 10}, {'name': 'Homer', 'age': 39}]
>>> print(new_data)
[{'name': 'Bart', 'age': 10}, {'name': 'Homer', 'age': 39}]
srikavineehari
  • 2,176
  • 1
  • 9
  • 19
7

I have been a big fan of a filter with lambda. However, it is not best option if you consider time complexity.

First option

sorted_list = sorted(list_to_sort, key= lambda x: x['name'])
# Returns list of values

Second option

list_to_sort.sort(key=operator.itemgetter('name'))
# Edits the list, and does not return a new list

Fast comparison of execution times

# First option
python3.6 -m timeit -s "list_to_sort = [{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}, {'name':'Faaa', 'age':57}, {'name':'Errr', 'age':20}]" -s "sorted_l=[]" "sorted_l = sorted(list_to_sort, key=lambda e: e['name'])"

1000000 loops, best of 3: 0.736 µsec per loop

# Second option
python3.6 -m timeit -s "list_to_sort = [{'name':'Homer', 'age':39}, {'name':'Bart', 'age':10}, {'name':'Faaa', 'age':57}, {'name':'Errr', 'age':20}]" -s "sorted_l=[]" -s "import operator" "list_to_sort.sort(key=operator.itemgetter('name'))"

1000000 loops, best of 3: 0.438 µsec per loop

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Bejür
  • 38
  • 1
  • 4
5

If performance is a concern, I would use operator.itemgetter instead of lambda as built-in functions perform faster than hand-crafted functions. The itemgetter function seems to perform approximately 20% faster than lambda based on my testing.

From https://wiki.python.org/moin/PythonSpeed:

Likewise, the builtin functions run faster than hand-built equivalents. For example, map(operator.add, v1, v2) is faster than map(lambda x,y: x+y, v1, v2).

Here is a comparison of sorting speed using lambda vs itemgetter.

import random
import operator

# Create a list of 100 dicts with random 8-letter names and random ages from 0 to 100.
l = [{'name': ''.join(random.choices(string.ascii_lowercase, k=8)), 'age': random.randint(0, 100)} for i in range(100)]

# Test the performance with a lambda function sorting on name
%timeit sorted(l, key=lambda x: x['name'])
13 µs ± 388 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

# Test the performance with itemgetter sorting on name
%timeit sorted(l, key=operator.itemgetter('name'))
10.7 µs ± 38.1 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

# Check that each technique produces the same sort order
sorted(l, key=lambda x: x['name']) == sorted(l, key=operator.itemgetter('name'))
True

Both techniques sort the list in the same order (verified by execution of the final statement in the code block), but the first one is a little faster.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
swac
  • 31
  • 1
  • 3