# How to sort a Dictionary by value in Python

**Date:** 2021-06-30

Sorting dictionaries by value is a common task in programming, and Python offers a straightforward way to achieve this.  This article will explain the concept of sorting dictionaries based on their values, clarifying the process without resorting to code examples.  We'll explore the core functionality and the underlying logic involved, ensuring a clear understanding for anyone interested in data manipulation and organization.

The fundamental challenge lies in the nature of dictionaries themselves.  Unlike lists, which inherently maintain an order, dictionaries are unordered collections of key-value pairs.  A key is a unique identifier, and a value is the associated data.  When you create a dictionary, the order in which you add key-value pairs might not be preserved.  Therefore, if you need to arrange the dictionary's contents based on the values, a sorting operation becomes necessary.

Python provides a built-in function, `sorted()`, which is crucial for this process. The `sorted()` function, in essence, takes a collection of items (in this case, a dictionary's contents) and returns a new sorted list. Importantly, the original dictionary remains unchanged; the `sorted()` function creates a separate, sorted list.

To sort a dictionary by value, we utilize the `sorted()` function in a specific way.  We can't directly sort the dictionary itself, as its structure doesn't inherently support ordering by values. Instead, we leverage the fact that the `sorted()` function accepts a `key` argument. This `key` argument allows us to specify a function that determines how the items should be compared and ordered.  For value-based sorting, we provide a lambda function – a small, anonymous function – that extracts the value associated with each key.  This lambda function acts as the sorting criterion, directing `sorted()` to organize the items based on the values rather than the keys.

The process can be visualized as follows: imagine the dictionary as a collection of cards, each with a key written on one side and a value on the other.  The `sorted()` function, along with the lambda function acting as the sorting key, examines each card, focusing only on the value (the data on one side). It then arranges the cards (creating a new, sorted collection of cards) based on the numerical or alphabetical order of the values.  This arrangement produces a sorted list of key-value pairs, ordered according to the values.

The outcome is a new, sorted list. This list contains tuples, where each tuple represents a key-value pair from the original dictionary. The tuples are arranged in ascending order (by default) based on the values.  If you need descending order, you can specify a `reverse=True` parameter within the `sorted()` function.  This simple addition flips the order of the sorted list.  This means the largest values will come first.  We emphasize that this sorted list is a distinct entity; the original dictionary remains in its initial (unordered) state.


While the `sorted()` function is powerful and effective,  understanding its behavior is essential. The function creates a completely new sorted list; the original dictionary is not modified. This is a crucial detail, especially in situations where you need to preserve the original data structure.  If the goal is to create a new, sorted representation of the dictionary's content, this is perfectly appropriate and efficient.  However, if the intention is to change the dictionary itself, a different approach is necessary.

The `sorted()` function itself isn't directly associated with a particular programming language; the underlying concept of sorting collections of data is fundamental to numerous programming paradigms.  While the specific syntax or function name might vary, the general principle of using a comparison key to specify the sorting criterion is common.


This method provides a flexible way to sort dictionaries, allowing you to adapt it based on the specific needs of your application.  The ability to choose between ascending and descending order provides further control, enhancing the versatility of this technique.   Whether you're organizing data for analysis, display, or other operations, the principle of sorting dictionaries by value using a `sorted()` function with a lambda function as the sorting key provides an efficient and convenient approach.  Remembering that the original dictionary remains unaltered after this sorting operation is key to understanding the function's behavior and avoiding potential programming errors.  The created sorted list offers a clear and organized view of the dictionary's data, ordered according to its values, providing a valuable tool for various data manipulation tasks.


**[Read more](https://examples.javacodegeeks.com/how-to-sort-a-dictionary-by-value-in-python/)**
