How to Sort a Dictionary by Kth Index Value in Python?


In Python, we can sort a dictionary by the value of a specific index, often referred to as kth index value by using various functions like Using the sorted() Function with a Lambda Function, Using the itemgetter() Function from the operator Module, Using a Custom Function with the sorted() Function, etc. In this article, we will understand these methods and how we can use them to sort a dictionary by kth index value in Python.

Method 1: Using the sorted() Function with a Lambda Function

This method utilizes the sorted() function with a lambda function as the key parameter to sort the dictionary based on the value at the desired index. It provides a straightforward approach to sorting a dictionary by the Kth index value.

Syntax

sorted_dict = dict(sorted(dictionary.items(), key=lambda x: x[index]))

Here, the sorted() function takes an iterable and returns a new sorted list. We pass the dictionary.items() as the iterable, and the key parameter allows us to specify the sorting criterion using a lambda function. In the lambda function, x represents each item in the dictionary, and x[index] refers to the value at the desired index.

Example

In the below example, we have a dictionary where each key corresponds to a list. We want to sort the dictionary based on the value at index 1 of each list, which represents the quantity of fruit. The lambda function lambda x: x[index] extracts the value at index 1 from each item in the dictionary, and the resulting dictionary is sorted based on these values. The output displays the sorted dictionary, with the items ordered by ascending quantity.

dictionary = {1: ['Apple', 50], 2: ['Banana', 30], 3: ['Orange', 40]}
index = 1
sorted_dict = dict(sorted(dictionary.items(), key=lambda x: x[index]))
print(sorted_dict)

Output

{1: ['Apple', 50], 2: ['Banana', 30], 3: ['Orange', 40]}

Method 2: Using the itemgetter() Function from the operator Module

The itemgetter() function from the operator module offers a clean and efficient solution to sort a dictionary. It provides a concise and optimized way to extract the value at the desired index and sort the dictionary accordingly.

Syntax

from operator import itemgetter

sorted_dict = dict(sorted(dictionary.items(), key=itemgetter(index)))

Here, the itemgetter() function returns a callable object that can be used as the key function for sorting. We pass the index as the parameter to itemgetter() to specify the desired index for sorting.

Example

In the below example, we import the itemgetter() function from the operator module. The itemgetter(index) creates a callable object that retrieves the value at index 1 from each dictionary item. By passing this as the key parameter to sorted(), we sort the dictionary based on these values. The output is the sorted dictionary, which is ordered by ascending quantity of fruits.

from operator import itemgetter

dictionary = {1: ['Apple', 50], 2: ['Banana', 30], 3: ['Orange', 40]}
index = 1
sorted_dict = dict(sorted(dictionary.items(), key=itemgetter(index)))
print(sorted_dict)

Output

{1: ['Apple', 50], 2: ['Banana', 30], 3: ['Orange', 40]}

Method 3: Using a Custom Function with the sorted() Function

We can create our own custom function which contains the sorted function to sort the dictionary. By defining a function that takes a dictionary and an index as parameters and using the sorted() function with a lambda function inside the function body, the custom method provides a modular approach to sorting a dictionary by the Kth index value.

Syntax

def sort_dict_by_index(dictionary, index):
    return dict(sorted(dictionary.items(), key=lambda x: x[index]))

sorted_dict = sort_dict_by_index(dictionary, index)

Here, a custom function, sort_dict_by_index() is created to contain the sorting logic. The function takes the dictionary and index as parameters and returns the sorted dictionary using the same approach as Method 1.

Example

In the below example, we define the sort_dict_by_index() function that takes a dictionary and an index as parameters. Inside the function, we use the same sorted() and lambda function approach as in Method 1. The function call sort_dict_by_index(dictionary, index) returns the sorted dictionary, which is then printed.

def sort_dict_by_index(dictionary, index):
    return dict(sorted(dictionary.items(), key=lambda x: x[index]))

dictionary = {1: ['Apple', 50], 2: ['Banana', 30], 3: ['Orange', 40]}
index = 1
sorted_dict = sort_dict_by_index(dictionary, index)
print(sorted_dict)

Output

{1: ['Apple', 50], 2: ['Banana', 30], 3: ['Orange', 40]}

Conclusion

In this article, we discussed how we can use Python methods and functions to sort a dictionary by a specific index value. The first method utilized the sorted() function with a lambda function, while the second method employed the itemgetter() function from the operator module. The third method involved creating a custom function encapsulating the sorting logic.

Updated on: 18-Jul-2023

120 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements