Display nested dictionary content sorted by key in Python
Given a nested dictionary like this:
dog_breeds = {
'Labrador Retriever': {'life_span': 14, 'male_weight': '36 Kg', 'female_weight': '32 Kg'},
'Beagle': {'life_span': 15, 'male_weight': '11 Kg', 'female_weight': '10 Kg'},
'German Shepherd': {'life_span': 13, 'male_weight': '40 Kg', 'female_weight': '32 Kg'},
'Jack Russell Terrier': {'life_span': 16, 'male_weight': '8 Kg', 'female_weight': '8 Kg'},
'Rottweiler': {'life_span': 10, 'male_weight': '60 Kg', 'female_weight': '48 Kg'}
}
here’s a way to display its content sorted by key:
[ print(f'A {key} can live up to {value["life_span"]} years. A male can weight up to {value["male_weight"]} and a female up to {value["female_weight"]}.')
for (key, value) in sorted(dog_breeds.items()) ]
Let’s break down what is happening in the block above:
print(f'Hello, {name}!')
print(f") is using formatted strings, so you can use variable names in the print statement. If name = 'Marcie'
, it would print ‘Hello, Marcie!’. This formatted string is available in Python 3.6 and up. I use this all_the_time because it looks very similar to JavaScript’s template literals (note the back ticks in JS instead of single quotes):
Console.log(`Hello, {name}`)
So in the example above we’re using keys and values from the dictionary as variables.
Next, the for loop:
for (key, value) in sorted(dog_breeds.items())
this is where we’re sorting the dictionary and deconstructing its key and value from .items().
We’re also using list comprehension (and that’s why everything is encompassed by square brackets and the for loop comes last) but this block could also be written like this:
for (key, value) in sorted(dog_breeds.items()):
print(f'A {key} can live up to {value["life_span"]} years. A male can weight up to {value["male_weight"]} and a female up to {value["female_weight"]}.')
The final output will look like this:
A Beagle can live up to 15 years. A male can weight up to 11 Kg and a female up to 10 Kg.
A German Shepherd can live up to 13 years. A male can weight up to 40 Kg and a female up to 32 Kg.
A Jack Russell Terrier can live up to 16 years. A male can weight up to 8 Kg and a female up to 8 Kg.
A Labrador Retriever can live up to 14 years. A male can weight up to 36 Kg and a female up to 32 Kg.
A Rottweiler can live up to 10 years. A male can weight up to 60 Kg and a female up to 48 Kg.
sorted can also take a ‘reverse’ argument:
for (key, value) in sorted(dog_breeds.items(), reverse=True)
which will reverse the output:
A Rottweiler can live up to 10 years. A male can weight up to 60 Kg and a female up to 48 Kg.
A Labrador Retriever can live up to 14 years. A male can weight up to 36 Kg and a female up to 32 Kg.
A Jack Russell Terrier can live up to 16 years. A male can weight up to 8 Kg and a female up to 8 Kg.
A German Shepherd can live up to 13 years. A male can weight up to 40 Kg and a female up to 32 Kg.
A Beagle can live up to 15 years. A male can weight up to 11 Kg and a female up to 10 Kg.
The post Display nested dictionary content sorted by key in Python was originally published at flaviabastos.ca