Home » Programming » Python » Python Iterate Loop Dictionary

How to Iterate/Loop over a Dictionary in Python [Examples]

This article will show you how to iterate/loop over the keys and values in a Python dictionary, and provide code examples.

What is a Python Dictionary?

In Python, a dictionary is a data type that contains a collection of objects – but unlike a list or array, rather than their values being recorded at a specific index (or ordered position in the sequence of stored values), they are stored in a key – a string identifier which both tells you what a value is for, and which can also be used to retrieve it.

You can read more about the dictionary data type in Python here.

Are Python Dictionaries Iterable/Can Dictionaries be Looped?

Yes! Python dictionaries are iterable, which means that the values stored within them can be looped over one-by-one in the sequence they were added to the dictionary, starting with the oldest first.

If you need to, you can re-order the sequence of values in a dictionary.

Iterating Over a Dictionary by Keys (Default Behaviour)

By default, when iterated over a dictionary, the key of each entry is used by the iterator:

dogDictionary = {
    "name": "rex",
    "breed": "bloodhound",
    "colour": "brown",
    "age": 5

Above, a dictionary named dogDictionary is defined, describing someone’s pet bloodhound dog. The keys in the dictionary can be looped over using a for loop:

for key in dogDictionary:

This will output only the keys in the dictionary:


Using the keys() Method

It is also possible to access the keys using the keys() method of the dictionary:

for key in dogDictionary.keys():

This will behave the same way as using the default method, but is more explicit in its purpose for when your are reading your code, making debugging easier.

Accessing a Dictionary’s Values Through it’s Keys

If you want to access the keys and values while looping through your dictionary, you can use the key in each iteration to access the value in the original dictionary using square-bracket ([]) notation:

for key in dogDictionary:

The above code will output:


Accessing a Dictionary’s Keys & Values using the items() Method

If you want to use both the key and value in your loop, you can grab the keys and values using the dictionary items() method:

for value in dogDictionary.items():

This will output:

('name', 'rex')
('breed', 'bloodhound')
('colour', 'brown')
('age', 5)

The key/value pairs will be available within the loop as tuples. This syntax makes it easier to work with the key/value pairs within the loop compared to the previously discussed methods.

Automatically Unpacking Tuples

To make it even easier to work with dictionaries, you can automatically unpack the values in the tuples and assign them to named variables using comma notation:

for key, value in dogDictionary.items():

Updating values while you iterate

When working with dictionaries, it is likely that at some stage you will need to update the values within them while iterating.

priceList = {
    "cricket bat": 43.1,
    "tennis racquet": 12.4,
    "inflatable pool": 100.0

for key, value in priceList.items():
    priceList[key] = value + 3

Above, a dictionary containing the product prices for a sporting goods store is defined. It is then looped over, adding 3 to each of the prices, updating the values in the original dictionary by their key.

Updating dictionary keys

Dictionary keys can be deleted using the del statement:

for key, value in priceList.items():
    if value > 50:
        del priceList[key]

Above, a key/value pair is completely removed from the dictionary if the value is greater than 50.

Photo of author
I'm Brad, and I'm nearing 20 years of experience with Linux. I've worked in just about every IT role there is before taking the leap into software development. Currently, I'm building desktop and web-based solutions with NodeJS and PHP hosted on Linux infrastructure. Visit my blog or find me on Twitter to see what I'm up to.

Leave a Comment