Ways to sort list of dictionaries by values in Python – Using itemgetter
Last Updated : 19 Aug, 2024
In this article, we will cover how to sort a dictionary by value in Python. To sort a list of dictionaries by the value of the specific key in Python we will use the following method in this article.
In everyday programming, sorting has always been a helpful tool. Python's dictionary is frequently utilized in a variety of applications, from those involving competition to those involving developers (e.g. handling JSON data). In these circumstances, being able to filter dictionaries according to their values can be helpful.
There are two methods for doing this sorting:
What is Itemgetter in Python?
The Itemgetter can be used instead of the lambda function to achieve similar functionality. Outputs in the same way as sorted() and lambda, but has different internal implementation. It takes the keys of dictionaries and converts them into tuples. It reduces overhead and is faster and more efficient. The "operator" module has to be imported for its work. The code is explained below
- Performance: itemgetter performs better than lambda functions in the context of time.
- Concise: : itemgetter looks more concise when accessing multiple values than lambda functions.
Example:
Python # Python code to demonstrate the working of sorted() # and itemgetter # importing "operator" for implementing itemgetter from operator import itemgetter # Initializing list of dictionaries data_list = [{"name": "Nandini", "age": 20}, {"name": "Manjeet", "age": 20}, {"name": "Nikhil", "age": 19}] # using sorted and itemgetter to print list sorted by age print("The list printed sorting by age: ") print(sorted(data_list, key=itemgetter('age'))) print("\r") # using sorted and itemgetter to print # list sorted by both age and name # notice that "Manjeet" now comes before "Nandini" print("The list printed sorting by age and name: ") print(sorted(data_list, key=itemgetter('age', 'name'))) print("\r") # using sorted and itemgetter to print list # sorted by age in descending order print("The list printed sorting by age in descending order: ") print(sorted(data_list, key=itemgetter('age'), reverse=True))
OutputThe list printed sorting by age: [{'name': 'Nikhil', 'age': 19}, {'name': 'Nandini', 'age': 20}, {'name': 'Manjeet', 'age': 20}] The list printed sorting by age and name: [{'name': 'Nikhil', 'age': 19}, {'name': 'Manjeet', 'age': 20}, {'name': 'Nandini', 'age': 20}] The list printed sorting by age in descending order: [{'name': 'Nandini', 'age': 20}, {'name': 'Manjeet', 'age': 20}, {'name': 'Nikhil', 'age': 19}]
Time complexity: O(n log n)
Auxiliary space: O(n)
Next Article -> Ways to sort list of dictionaries by values in Python - Using lambda function
Similar Reads
Ways to sort list of dictionaries by values in Python - Using lambda function In this article, we will cover how to sort a dictionary by value in Python. Sorting has always been a useful utility in day-to-day programming. Dictionary in Python is widely used in many applications ranging from competitive domain to developer domain(e.g. handling JSON data). Having the knowledge
2 min read
Iterate through list of dictionaries in Python In this article, we will learn how to iterate through a list of dictionaries. List of dictionaries in use: [{'Python': 'Machine Learning', 'R': 'Machine learning'}, {'Python': 'Web development', 'Java Script': 'Web Development', 'HTML': 'Web Development'}, {'C++': 'Game Development', 'Python': 'Game
3 min read
Sort Python Dictionary by Key or Value - Python There are two elements in a Python dictionary-keys and values. You can sort the dictionary by keys, values, or both. In this article, we will discuss the methods of sorting dictionaries by key or value using Python.Sorting Dictionary By Key Using sort()In this example, we will sort the dictionary by
6 min read
Python - Sort list of list by specified index When working with a list of lists, we often need to sort the inner lists based on the values at a specific index. Sorting by a specified index is useful in the custom ordering of multidimensional data. In this article, we will explore different ways to sort a list of lists by a specified index in Py
3 min read
Python | Sort the list alphabetically in a dictionary In Python Dictionary is quite a useful data structure, which is usually used to hash a particular key with value, so that they can be retrieved efficiently. Let's see how to sort the list alphabetically in a dictionary. Sort a List Alphabetically in PythonIn Python, Sorting a List Alphabetically is
3 min read