Well, the first part of this mini-series introduced you to some of the core concepts of how to sort python list. This second part will expound on that, by going a step further, with a little bit complex example.
If you recall, the last sort python list mini-series worked with a simple list of numbers. BUT it was important, since it got to introduce you to the very basics of how to go about with your python sorting. If you haven’t checked it out, please do so, because it will help you understand some of the concepts here.
So, just like the previous example, we will have this in two parts, defining our data and then the next part will cover how to sort python list of python dictionary.
In this example, we will have a python dictionary list of hypothetical contents per 1kg of different foods servings.
For this, our python dictionary will look like so:
"""
{
'name': {string},
'calories_in_cals_per_kg': {number},
'proteins_in_grams_per_kg': {number}
}
"""
As you can see, it is really straight forward.
The name is just the name of the serving, calories_in_cals_per_kg are how many calories we can get from 1 kg of that food serving, and proteins_in_grams_per_kg is the protein amount per 1kg serving.
Of course you can add other hypothetical attributes and values as well.
Now that we know the structure of the data we are going to work with to sort python dictionary list, let’s declare our data:
foods = [
{
'name': 'Maize Flour',
'calories_in_cals_per_kg': 88,
'proteins_in_grams_per_kg': 43
},
{
'name': 'Rice',
'calories_in_cals_per_kg': 105,
'proteins_in_grams_per_kg': 55
},
{
'name': 'Yams',
'calories_in_cals_per_kg': 97,
'proteins_in_grams_per_kg': 87
},
{
'name': 'Beans',
'calories_in_cals_per_kg': 50,
'proteins_in_grams_per_kg': 111
}
]
Okay, now that the data is ready, let’s begin learning how to sort python dictionary list.
Since we are going to be using only python methods, we will be using the famous itemgetter from operator module.
So, we import it:
from operator import itemgetter
Now we are ready to use itemgetter.
Now that we have imported itemgetter into our script, we introduce it to python sorted method like so:
sorted(list_to_sort, key=itemgetter('attribute_to_use'))
This is the default of how to use python sorted to sort python dictionary list. Notice that we are passing in the attribute_to_use as a key into the python sorted method.
So, let’s try and sort our foods based on the calories:
sort_foods_based_on_calories = sorted(foods, key=itemgetter('calories_in_cals_per_kg'))
print(sort_foods_based_on_calories)
What this will do is that it will sort python list of dictionaries which we pass into it, and by DEFAULT it will sort in ascending order, BASED on the specified attribute.
Okay, now, let’s change the challenge and say we want to instead sort python list provided, from one with highest calories to the lowest calories.
sort_foods_based_on_highest_calories = sorted(foods, key=itemgetter('calories_in_cals_per_kg'), reverse=True)
print(sort_foods_based_on_highest_calories)
Well, notice the reverse parameter, if you don’t know what it does, simply find the link at the bottom for the previous article which introduce how to sort python list.
Okay, so, if you want to have the list sorted by the highest proteins per kg, then our python sorted method will be like so:
sort_foods_based_on_highest_proteins = sorted(foods, key=itemgetter('proteins_in_grams_per_kg'), reverse=True)
print(sort_foods_based_on_highest_proteins)
Well, it is that simple!
Okay, what if you try to sort by an attribute which is not there? Well, you will receive python KeyError showing you the missing attribute you are trying to sort by.
Okay, as you can see, it is not that difficult to sort python list of dictionaries. So, just like the previous article, we have a couple of key concepts you should take from this tutorial.
Well, that is it from this article.
If some of the concepts introduced here, such as how to use python sorted to sort python list, are unclear, simply find the full introduction in the previous article.