Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
10327 views
ubuntu2004
Kernel: Python 3 (system-wide)

Dictionaries II - looping

Looping through a dictionary

We've seen how to loop through strings and lists. Now we will look at the ways of looping through a dictionary one item at a time.

The following code loops through the keys of our dictionary of white blood cells.

Run it to see what happens.
white_cells = { 'Neutrophil':11, 'Eosinophil':11, 'Basophil':13.5, 'Small lymphocyte':7.5, 'Large lymphocyte':13.5, 'Monocyte':22.5 } # Loop through the keys one at a time using cell_type as the iterating variable. for cell_type in white_cells: # Print each key. print( cell_type )
Neutrophil Eosinophil Basophil Small lymphocyte Large lymphocyte Monocyte

Notice that this code only loops through the keys. The values are ignored.

Looping through keys and values simultaneously

Often we want to loop through a dictionary and use or output each key:value pair. There are two of ways of doing this:

  1. Loop through the keys and use them to get each item's value.

  2. Loop through the items.

Let's look at these in turn.

1. Loop through the keys and use them to get each item's value

In the last Notebook we saw that to get the value of a key:value pair we use the notation dict_var[key]. So we could modify the above code to output each key and its value like so:

for cell_type in white_cells: # Use the key in cell_type to get its corresponding diameter. diameter = white_cells[cell_type] # Print each key and its value in an f-string. print( f'The average diameter of a {cell_type} is {diameter} micrometers' )
The average diameter of a Neutrophil is 11 micrometers The average diameter of a Eosinophil is 11 micrometers The average diameter of a Basophil is 13.5 micrometers The average diameter of a Small lymphocyte is 7.5 micrometers The average diameter of a Large lymphocyte is 13.5 micrometers The average diameter of a Monocyte is 22.5 micrometers

2. Loop through items

A simpler and shorter way to write the above code is by looping through the items of the dictionary rather than just the keys.

Run the following code to see how to do this.
# Loop through each item in the dictionary. # There are two iterating variables: cell_type and diameter. for cell_type, diameter in white_cells.items(): print( f'The average diameter of a {cell_type} is {diameter} micrometers' )

There is some new notation. The line

for cell_type, diameter in white_cells.items()

loops through each item in the dictionary. But now there are two iterating variables: cell_type which contains the key of the item, and diameter which contains the value of the item.

This is a common way of looping over a dictionary when both the key and its value are needed.

Loop through sorted keys

We may want to loop through the keys in alphabetical or numerical order. This is achieved by using sorted() which we came across in Notebook 12 when we looped through a sorted list.

Run the following code to see how this works.

Notice that the cell types are sorted alphabetically.

# Loop through each item in the dictionary with keys sorted alphabetically for cell_type, diameter in sorted( white_cells.items() ): print( f'The average diameter of a {cell_type} is {diameter} micrometers' )
The average diameter of a Basophil is 13.5 micrometers The average diameter of a Eosinophil is 11 micrometers The average diameter of a Large lymphocyte is 13.5 micrometers The average diameter of a Monocyte is 22.5 micrometers The average diameter of a Neutrophil is 11 micrometers The average diameter of a Small lymphocyte is 7.5 micrometers

Loop through reverse sorted keys

The following code shows how to loop through the keys sorted in reverse alphabetical order.

We've added

reverse=True

into sorted().

# Loop through each item in the dictionary with keys sorted in reverse alphabetical order for cell_type, diameter in sorted(white_cells.items(), reverse=True): print( f'The average diameter of a {cell_type} is {diameter} micrometers' )
The average diameter of a Small lymphocyte is 7.5 micrometers The average diameter of a Neutrophil is 11 micrometers The average diameter of a Monocyte is 22.5 micrometers The average diameter of a Large lymphocyte is 13.5 micrometers The average diameter of a Eosinophil is 11 micrometers The average diameter of a Basophil is 13.5 micrometers

Loop through sorted values: Advanced Python

Rather than loop through the dictionary sorted on keys we may want to loop through the dictionary sorted on their values, either alphabetically or numerically.

Run the following code to see how this works.
# Loop through each item in the dictionary with values sorted numerically for cell_type, diameter in sorted(white_cells.items(), key=lambda kv: kv[1]): print( f'The average diameter of a {cell_type} is {diameter} micrometers' )
The average diameter of a Small lymphocyte is 7.5 micrometers The average diameter of a Neutrophil is 11 micrometers The average diameter of a Eosinophil is 11 micrometers The average diameter of a Basophil is 13.5 micrometers The average diameter of a Large lymphocyte is 13.5 micrometers The average diameter of a Monocyte is 22.5 micrometers

Notice that Small lymphocyte comes first at 7.5 micrometers and Monocyte comes last at 22.5 micrometers.

We won't explain here how this works as it is beyond the scope of this course.

Exercise Notebook

Next Notebook