How to sort a dictionary in Python

Published on Aug. 22, 2023, 12:15 p.m.

To sort a dictionary by its values in Python

To sort a dictionary by its values in Python, you can use the sorted() function with the key argument. Here’s an example:

my_dict = {'apple': 3, 'banana': 2, 'cherry': 1}

sorted_dict = sorted(my_dict.items(), key=lambda x: x[1])

print(sorted_dict)

This will output:

[('cherry', 1), ('banana', 2), ('apple', 3)]

In this example, we have a dictionary my_dict with string keys and integer values. We pass my_dict.items() to the sorted() function to sort the dictionary by its values. The key argument specifies a lambda function that returns the values of the dictionary items. The resulting sorted_dict is a list of tuples containing the key-value pairs of the original dictionary, sorted by their values in ascending order.

Alternatively, you can use the items() method of the dictionary to get a list of its items, sort the list using the key argument, and then create a new dictionary from the sorted list. Like this:

my_dict = {'apple': 3, 'banana': 2, 'cherry': 1}

sorted_list = sorted(my_dict.items(), key=lambda x: x[1])

sorted_dict = dict(sorted_list)

print(sorted_dict)

This will output:

{'cherry': 1, 'banana': 2, 'apple': 3}

In this example, we create a sorted list of items from the original dictionary using the sorted() function as before. We then create a new dictionary from the sorted list using the dict() constructor, which takes in a list of key-value pairs as its argument.

To sort a dictionary by its keys in Python

To sort a dictionary by its keys in Python, you can use either the sorted() function or the items() method of the dictionary. Here’s an example using sorted():

my_dict = {'apple': 3, 'banana': 2, 'cherry': 1}

sorted_dict = dict(sorted(my_dict.items()))

print(sorted_dict)

This will output:

{'apple': 3, 'banana': 2, 'cherry': 1}

In this example, we pass my_dict.items() to the sorted() function to sort the dictionary by its keys. Since the default behavior of sorted() is to sort by keys, we don’t need to specify a key argument. We then create a new dictionary from the sorted list of key-value pairs using the dict() constructor.

Alternatively, you can use the items() method of the dictionary to get a list of its items, sort the list using the key argument, and then create a new dictionary from the sorted list. Like this:

my_dict = {'apple': 3, 'banana': 2, 'cherry': 1}

sorted_list = sorted(my_dict.items())

sorted_dict = dict(sorted_list)

print(sorted_dict)

This will output the same result as before:

{'apple': 3, 'banana': 2, 'cherry': 1}

In this example, we create a sorted list of items from the original dictionary using the sorted() function as before. We then create a new dictionary from the sorted list using the dict() constructor.

To sort a list in Python

To sort a list in Python, you can use the sort() method or the sorted() function. Here are some examples:

Using the sort() method:

my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]

my_list.sort()

print(my_list)

Output:

[1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]

Using the sorted() function:

my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]

sorted_list = sorted(my_list)

print(sorted_list)

Output:

[1, 1, 2, 3, 3, 4, 5, 5, 5, 6, 9]

In both examples, we have a list my_list with integer values. The sort() method sorts the list in ascending order, while the sorted() function returns a new sorted list without modifying the original list.

You can also pass an optional reverse=True argument to the sort() method or sorted() function to sort the list in descending order. For example:

my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]

my_list.sort(reverse=True)

print(my_list)

Output:

[9, 6, 5, 5, 5, 4, 3, 3, 2, 1, 1]
my_list = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]

sorted_list = sorted(my_list, reverse=True)

print(sorted_list)

Output:

[9, 6, 5, 5, 5, 4, 3, 3, 2, 1, 1]

Tags: