Ways to sort list of dictionaries by values in Python – Using itemgetter Last Updated : 19 Aug, 2024 Comments Improve Suggest changes Like Article Like Report 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 Comment More infoAdvertise with us Next Article Ways to sort list of dictionaries by values in Python – Using itemgetter M manjeet_04 Follow Improve Article Tags : Sorting Python DSA python-dict Python dictionary-programs +1 More Practice Tags : pythonpython-dictSorting 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 Sort a List According to the Length of the Elements - Python We are given a list of strings and our task is to sort the list based on the length of each string. For example, if the input is ["Python", "C", "Java", "JavaScript", "Go"], the output should be ["C", "Go", "Java", "Python", "JavaScript"] since shorter strings come first.Using sorted() sorted() func 3 min read Sort a list of strings in lexicographical order in Python We are given a list of strings and our task is to sort them in lexicographical order, which means alphabetical order either from A to Z (ascending) or Z to A (descending). This is a common operation when organizing or comparing strings in Python. For example:Input: ["banana", "apple", "cherry", "dat 2 min read Python - Keys associated with value list in dictionary Sometimes, while working with Python dictionaries, we can have a problem finding the key of a particular value in the value list. This problem is quite common and can have applications in many domains. Let us discuss certain ways in which we can Get Keys associated with Values in the Dictionary in P 4 min read Scraping And Finding Ordered Words In A Dictionary using Python What are ordered words? An ordered word is a word in which the letters appear in alphabetic order. For example abbey & dirt. The rest of the words are unordered for example geeksThe task at hand This task is taken from Rosetta Code and it is not as mundane as it sounds from the above description 3 min read How to Sort a Set of Values in Python? Sorting means arranging the set of values in either an increasing or decreasing manner. There are various methods to sort values in Python. We can store a set or group of values using various data structures such as list, tuples, dictionaries which depends on the data we are storing. We can sort val 7 min read Like