Sort Dictionary in Python W3Schools Guide

Logan Jul 13, 2026

Python, renowned for its simplicity and readability, offers powerful tools for data manipulation and analysis. One such tool is the built-in 'dict' type, which stores key-value pairs. But, by default, dictionaries in Python are sequence types, and their order remained unchanged until Python 3.7. Starting from this version, Python's default dictionaries maintain the insertion order, making it possible to sort them based on their keys or values.

Python Sort Dictionary by Key and Value with Example
Python Sort Dictionary by Key and Value with Example

This article will guide you through sorting dictionaries in Python, leveraging a variety of methods, including using built-in functions, the 'itemgetter' and 'lambda' functions, and the newer 'SortedDict' from 'collections' module. We'll also explore sorting in numerical and alphabetical orders, as well as reverse sorting.

Python Dictionary Methods
Python Dictionary Methods

Sorting Dictionaries Based on Keys

In this section, we'll discuss several methods to sort dictionaries based on their keys.

an image of a computer screen with the text, day 11 / 15 sot dictionary
an image of a computer screen with the text, day 11 / 15 sot dictionary

For Python 3.7 and later, you can simply use the built-in 'sorted' function with the 'items' method of the dictionary:

```python sorted_dict = sorted(my_dict.items()) ```

Using the 'itemgetter' Function

a screenshot of a computer screen with the text looping through dictionaries in python
a screenshot of a computer screen with the text looping through dictionaries in python

The 'itemgetter' function from the 'operator' module can also be used for sorting. It is a more explicit but sometimes clearer approach:

```python from operator import itemgetter sorted_dict = sorted(my_dict.items(), key=itemgetter(0)) ```

Using a 'lambda' Function

A 'lambda' function can also be used for a more concise syntax:

Python Dictionaries Explained in the Simplest Way Possible
Python Dictionaries Explained in the Simplest Way Possible

```python sorted_dict = sorted(my_dict.items(), key=lambda item: item[0]) ```

Sorting Dictionaries Based on Values

To sort dictionaries based on their values, just specify the column as the second element in the 'itemgetter' or 'lambda' function:

Revisiting 'itemgetter'

Looping Through Dictionaries in Python 🔑
Looping Through Dictionaries in Python 🔑

With the 'itemgetter', you can sort by values like so:

```python sorted_dict = sorted(my_dict.items(), key=itemgetter(1)) ```

And 'lambda'

Python Dictionary Tutorial: Analyze Craft Beer with Dictionaries – Dataquest
Python Dictionary Tutorial: Analyze Craft Beer with Dictionaries – Dataquest
Python Add to Dictionary: How to Append key value pairs in Dictionary
Python Add to Dictionary: How to Append key value pairs in Dictionary
an image of a computer screen with the words'multi - value dictionary in python '
an image of a computer screen with the words'multi - value dictionary in python '
Day 21/30 of learning python 📊📚  follow for more easy tech tips for beginners 💥💻🚀
Day 21/30 of learning python 📊📚 follow for more easy tech tips for beginners 💥💻🚀
a black background with colorful text that says,'python dictionaries cheat sheet '
a black background with colorful text that says,'python dictionaries cheat sheet '
a screenshot of a black screen with text that reads interesting fact about dictionaries
a screenshot of a black screen with text that reads interesting fact about dictionaries
a screenshot of a black screen with text that reads 7 sort by value 8 nested dict access with get
a screenshot of a black screen with text that reads 7 sort by value 8 nested dict access with get
a screenshot of a screen with text that reads dictionary comprehension 2 get with default
a screenshot of a screen with text that reads dictionary comprehension 2 get with default
Python Print Dictionary Techniques And Examples
Python Print Dictionary Techniques And Examples
a poster with some words on it that say, what is dictionary comprehen
a poster with some words on it that say, what is dictionary comprehen
Python Dictionary Methods Cheat Sheet | dict() Functions Explained for Beginners & Interviews
Python Dictionary Methods Cheat Sheet | dict() Functions Explained for Beginners & Interviews
python
python
an image of a screen with the words ordered dictionaries
an image of a screen with the words ordered dictionaries
Python Dictionaries Explained (With Real Examples)
Python Dictionaries Explained (With Real Examples)
List vs Tuple vs Set vs Dictionary in Python (Beginner's Guide)
List vs Tuple vs Set vs Dictionary in Python (Beginner's Guide)
python dictionary print - print data structures in python
python dictionary print - print data structures in python
Is Dictionary Mutable in Python
Is Dictionary Mutable in Python
Dictionaries in Python 💻
Dictionaries in Python 💻
How Python Lambda Sorted with Advanced Examples
How Python Lambda Sorted with Advanced Examples
an image of a book cover with the title level 5 dictionary comprehenons
an image of a book cover with the title level 5 dictionary comprehenons

Similarly, using a 'lambda' function gives:

```python sorted_dict = sorted(my_dict.items(), key=lambda item: item[1]) ```

Sorting in Reverse Order

To sort in reverse order, apply the 'reverse' parameter in the 'sorted' function:

```python sorted_dict = sorted(my_dict.items(), key=itemgetter(0), reverse=True) ```

Sorting Alphanumerically

When dealing with mixed alphanumeric values, you may want to handle sorting as is commonly done in databases, which is by sorting both alphabetic and numeric components together. You can do this by using the 'itemgetter' or 'lambda' option to dictate the order of sorting, but the natural order feature in Python 3.7+ tackles it natively:

```python sorted_dict = sorted(my_dict.items(), key=lambda item: (str(item[1]), item[0])) ```

The 'SortedDict' Class

The 'SortedDict' class from the 'collections' module provides more functionality and enhances dictionary usage with ordered items. It maintains the insertion order and can be sorted efficiently:

```python from collections import SortedDict sorted_dict = SortedDict(my_dict) ```

This concludes our journey through sorting dictionaries in Python. With several methods, you can tailor sorting to your needs. Happy coding!