# Sort dictionary by key/value in Python

A Python dictionary is a built-in data type that represents a collection of**key-value pairs,**where each key is associated with a value. It is an unordered, mutable, and iterable data structure that can store any type of data as keys and values. The keys of a dictionary are unique,

**immutable objects,**while the values can be of any type and can be repeated.

## Sort a dictionary by value | Ascending/Descending

To sort a dictionary by its values in Python, you can use the sorted function with a**lambda function**as the key argument.

# define a dictionary
color_dict = {'red': 10, 'blue': 5, 'green': 20, 'yello': 15}
# sort the dictionary by value in ascending order
sorted_dict_asc = dict(sorted(color_dict.items(), key=lambda x: x[1]))
# sort the dictionary by value in descending order
sorted_dict_desc = dict(sorted(color_dict.items(), key=lambda x: x[1], reverse=True))
# print the sorted dictionaries
print(sorted_dict_asc)
print(sorted_dict_desc)

# Output:
{'blue': 5, 'red': 10, 'yello': 15, 'green': 20}
{'green': 20, 'yello': 15, 'red': 10, 'blue': 5}

In the above example, two sorted dictionaries are created: sorted_dict_asc is sorted by value in ascending order, and sorted_dict_desc is sorted by value in descending order. The sorted function is used with a lambda function as the key argument to specify the **sorting criterion.**The lambda function takes each key-value pair as an input (x) and returns its value (x[1]). If the reverse parameter is True, the sorting is done in descending order, otherwise it's done in ascending order. Finally, the sorted key-value pairs are converted back into dictionaries using the dict function.

## Sort a dictionary by key | Ascending/Descending

To sort a dictionary by its keys in ascending or descending order in Python, you can use the sorted function with the items method of the dictionary as the argument, and specify the**reverse parameter**as True or False. Here's an example:

# define a dictionary
color_dict = {'red': 10, 'blue': 5, 'green': 20, 'yello': 15}
# sort the dictionary by key in ascending order
sorted_dict_asc = dict(sorted(color_dict.items()))
# sort the dictionary by key in descending order
sorted_dict_desc = dict(sorted(color_dict.items(), reverse=True))
# print the sorted dictionaries
print(sorted_dict_asc)
print(sorted_dict_desc)

# Output:
{'blue': 5, 'green': 20, 'red': 10, 'yello': 15}
{'yello': 15, 'red': 10, 'green': 20, 'blue': 5}

In the above example, two sorted dictionaries are created: sorted_dict_asc is sorted by key in ascending order, and sorted_dict_desc is sorted by key in descending order. The sorted function is used with the items method of the dictionary as the argument to specify the **sorting criterion.**If the reverse parameter is True, the sorting is done in descending order, otherwise it's done in ascending order. Finally, the sorted key-value pairs are converted back into dictionaries using the dict function.

## How dictionary Sorting Algorithm works

The sorting algorithm for dictionaries in Python works differently for sorting by keys versus sorting by values.

When sorting a dictionary by keys, Python simply iterates over the

**dictionary keys**and sorts them in ascending or descending order based on the specified criteria. This can be achieved by using the

**sorted() function,**which returns a list of the sorted keys. Once you have the sorted keys, you can use them to access the corresponding values in the dictionary in the order you desire. When sorting a dictionary by values, the process is a bit more involved. Since Python dictionaries are inherently unordered, the values need to be extracted from the dictionary and sorted independently. To do this, you can use the

**items() method**of the dictionary to create a list of key-value pairs, which can then be sorted based on the value. The

**sorted() function**can again be used to accomplish this. Once the key-value pairs have been sorted by value, they can be used to create a new dictionary with the desired order. In both cases, the

**sorted() function**uses a comparison function that determines the order of the keys or values. By default, the comparison function uses the

**less-than operator (<)**to determine the order, but this can be customized by passing a key argument to the sorted() function.

## What is lambda function

A**lambda function in Python**is a small anonymous function that can take any number of arguments, but can only have one expression. It is created using the keyword lambda, followed by a comma-separated list of arguments, and then the expression to be evaluated.

**Lambda functions**are often used as a shortcut for defining simple functions that are only used once, or as arguments to higher-order functions that expect a function as input.

**Related Topics**