Modify a dictionary while iterating over it in Python

avatar

Borislav Hadzhiev

Last updated: Sep 20, 2022

banner

Photo from Unsplash

Modify a dictionary while iterating over it in Python #

To modify a dictionary while iterating over it:

  1. Use the dict.copy() method to iterate over a copy of the dictionary.
  2. On each iteration, check if a condition is met.
  3. If the condition is met, modify the dictionary.
main.py
my_dict = { 'first_name': 'bobby', 'last_name': 'hadz', 'site': 'bobbyhadz.com', 'topic': 'Python' } for key in my_dict.copy(): if my_dict[key] == 'Python': del my_dict[key] my_dict['new_key'] = 'new_value' # 👇️ {'first_name': 'bobby', 'last_name': 'hadz', 'site': 'bobbyhadz.com', 'new_key': 'new_value'} print(my_dict)

We used the dict.copy() method to get a copy of the dictionary.

The dict.copy method returns a shallow copy of the object on which the method was called.

main.py
my_dict = { 'first_name': 'bobby', 'last_name': 'hadz', 'site': 'bobbyhadz.com', 'topic': 'Python' } # 👇️ {'first_name': 'bobby', 'last_name': 'hadz', 'site': 'bobbyhadz.com', 'topic': 'Python'} print(my_dict.copy())
This is necessary because we aren't allowed to modify a dictionary while iterating over it.

However, we can iterate over a copy of the dictionary and modify the items of the original dictionary.

On each iteration, we check if the current value is equal to a specific string.

If the condition is met, we can use the del operator to remove key-value pairs from the dictionary or add new key-value pairs.

We could also iterate over a copy of the dictionary's items.

main.py
my_dict = { 'first_name': 'bobby', 'last_name': 'hadz', 'site': 'bobbyhadz.com', 'topic': 'Python' } for key, value in my_dict.copy().items(): if value == 'Python': del my_dict[key] my_dict['new_key'] = 'new_value' # 👇️ {'first_name': 'bobby', 'last_name': 'hadz', 'site': 'bobbyhadz.com', 'new_key': 'new_value'} print(my_dict)

The dict.items method returns a new view of the dictionary's items ((key, value) pairs).

main.py
my_dict = { 'first_name': 'bobby', 'last_name': 'hadz', 'site': 'bobbyhadz.com', 'topic': 'Python' } # 👇️ dict_items([('first_name', 'bobby'), ('last_name', 'hadz'), ('site', 'bobbyhadz.com'), ('topic', 'Python')]) print(my_dict.items())

Alternatively, you can use the list() class.

Modify a dictionary while iterating over it using list() #

To modify a dictionary while iterating over it:

  1. Use the list() class to convert the dictionary to a list of keys.
  2. Use a for loop to iterate over the list of keys.
  3. Modify the dictionary if a certain condition is met.
main.py
my_dict = { 'first_name': 'bobby', 'last_name': 'hadz', 'site': 'bobbyhadz.com', 'topic': 'Python' } for key in list(my_dict): if my_dict[key] == 'Python': del my_dict[key] my_dict['new_key'] = 'new_value' # 👇️ {'first_name': 'bobby', 'last_name': 'hadz', 'site': 'bobbyhadz.com', 'new_key': 'new_value'} print(my_dict)

We used the list() class to convert the dictionary to a list of keys.

main.py
my_dict = { 'first_name': 'bobby', 'last_name': 'hadz', 'site': 'bobbyhadz.com', 'topic': 'Python' } print(list(my_dict)) # 👉️ ['first_name', 'last_name', 'site', 'topic'] print(list(my_dict.keys())) # 👉️ ['first_name', 'last_name', 'site', 'topic']

We could have also used the dict.keys() method to be more explicit.

The dict.keys method returns a new view of the dictionary's keys.

On each iteration, we check if a certain condition is met and if it is, we modify the dictionary.

Instead of iterating over the dictionary, we iterate over the list of keys.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.