How to Sort a Dictionary Alphabetically or Numerically in Python



Python


In this article, we sort a dictionary alphabetically or numerically in Python.

We can sort either the values in order from least to greatest, either alphabetically or numerically, or we can sort the keys in order from least to greatest, either alphabetically or numerically.

We can do this in Python using a combination of the sorted() function and the zip() function.

Below we have a dictionary that contains some common beverages along with the price of each beverage.

We sort the prices of the beverages numerically from least to greatest, showing the cheapest beverages first followed by the more expensive beverages.

We then show how to sort the keys (name of the beverages) alphabetically just to show how to do this.

This is shown in the code below.



So we have a dictionary, beverages, which has some common beverages along with their price.

In order to sort the values (prices) of this dictionary from least to greatest, we use the line, beverages_sorted_by_price= sorted(zip(beverages.values(),beverages.keys()))

The sorted() function used with the zip() function, with beverages.values() listed as the first parameter to the zip() function, sorts the values of the dictionary from least to greatest. If you put the beverages.keys() value first in the zip() function, the keys will be sorted from least to greatest; this is what we do next.

We store the values sorted numerically from least to greatest in the variable, beverages_sorted_by_price

So after running the line, beverages_sorted_by_price, we get the beverages sorted from the cheapest price to the most expensive price.

Next, just to show that we can sort the keys also from least to greatest, we sort them alphabetically (since they are strings).

This is done by specifying the beverages.keys() as the first parameter to the zip() function and having the beverages.values() as the second parameter.

We store this in the variable, beverages_sorted_alphabetically

So after running the line, beverages_sorted_alphabetically, we get the beverages sorted alphabetically.



How to Sort a Dictionary In Reverse Alphabetically or Numerically

We will now show how to sort a dictionary in reverse alphabetically or numerically.

This means that can sort a dictionary, either the values or the keys, from greatest to least, either alphabetically or numerically.

To do this, we use the same sorted() function but specify as a parameter to this function, reverse= True

We use the same dictionary that we use before with the same exact keys and values .

But now we sort the dictionary in reverse, from greatest to least, alphabetically and numerically.

This is shown in the code below.



So with using the parameter, reverse= True, with the sorted() function, we sort the dictionary in alphabetically or numerically in reverse, from greatest to least.

We sort the prices from greatest to least, numerically.

We then sort the beverages from greatest to least, alphabetically.

So we can sort a dictionary alphabetically or numerically in Python either from least to greatest or greatest to least (in reverse).

And this is how to sort a dictionary alphabetically or numerically in Python.


Related Resources

How to Create a Zip File in Python

How to Extract All Files and Folders from a Zip File in Python

How to Read the Contents of a Zip File in Python



HTML Comment Box is loading comments...