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
Top comments (0)