Display nested dictionary content sorted by key in Python
Tags: python, dictionary, sort
Published: 2019-03-08 10:15:54

---
title: Display nested dictionary content sorted by key in Python
published: true
tags: python,dictionary,sort
canonical_url: https://flaviabastos.ca/2019/03/08/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](https://docs.python.org/3/reference/lexical_analysis.html#f-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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/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](https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions) (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](https://wp.me/pa0b0y-3a)_ was originally published at _[flaviabastos.ca](https://flaviabastos.ca/)