How To Use Python Sort List Of Dictionaries?

python sort list of dictionaries

Sorting a list in programming is a complex task for beginners. Therefore, today we will show you some ways to use Python sort list of dictionaries using the sorted() function.

How to use Python sort list ofdictionaries?

There are two dimensions to sorting a list. One is ascending, and the other is descending. In this article, we will guide you in the ascending direction. You can do the opposite in the descending direction.

Using the sorted() function and lambda function

We’ve covered the Lamda function’s syntax in a previous post. If you want to read it, you can read it here.

The sorted() function is a built-in function in Python. As the name implies, this function sorts the elements in a list, set, or tuple. About its syntax, you can visit here to see it.

To sort a list of dictionaries, we must pass that list to the first argument of the sorted() function. The second argument is a lambda function that returns the value used to determine the order within the Dictionary. The third parameter is the order of the list. If true, it will sort in descending order. By default, it will sort in ascending order.

It’s a bit confusing, but you’ll understand right away if you look at the example below:

employees = [
  {'name': 'Carlos', 'age': 30},
  {'name': 'Maze', 'age': 19},
  {'name': 'Nayeli', 'age': 24}
]

# Sort employee list by age using sorted() and lambda function
sortedEmployees = sorted(employees, key=lambda employee: employee['age'], reverse=False)
print(sortedEmployees)

Output:

[
  {'name': 'Maze', 'age': 19},
  {'name': 'Nayeli', 'age': 24},
  {'name': 'Carlos', 'age': 30}
]

Using the sorted() function and itemgetter() function

The itemgetter() is used to get items at specific indexes from an iterable.

Syntax:

itemgetter(positions)

Parameter:

  • positions: the indexes or keys to get in an iterable.

This function will return a getter.

We can use the itemgetter() function instead of the lambda function to achieve the same functionality. Since itemgetter() it will return a getter function, it is perfectly valid for the parameter key in sorted().

We can decide which value to sort by passing that value’s key to the itemgetter() function. See the example below to understand.

# Import itemgetter from operator module
from operator import itemgetter

employees = [
  {'name': 'Carlos', 'age': 30},
  {'name': 'Maze', 'age': 19},
  {'name': 'Nayeli', 'age': 24}
]

# Sort employee list by age using sorted() and itemgetter()
sortedEmployees = sorted(employees, key=itemgetter('age'), reverse=False)
print(sortedEmployees)

Output:

[
  {'name': 'Maze', 'age': 19},
  {'name': 'Nayeli', 'age': 24},
  {'name': 'Carlos', 'age': 30}
]

Notice: since itemgetter() is a function in the operator module, we must import it before using it.

Summary

Above are the two most common ways to Python sort list of dictionaries. Many examples show that using itemgetter() is faster than using the lambda function, but we think the difference is negligible. Just choose the way that is most convenient for you.

Thank you for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *