Reset all values in a Dictionary in Python

avatar

Borislav Hadzhiev

Last updated: Sep 17, 2022

banner

Photo from Unsplash

Reset all values in a Dictionary in Python #

Use the dict.fromkeys() method to reset all keys in a dictionary, e.g. my_dict = dict.fromkeys(my_dict, None). The dict.fromkeys() method creates a new dictionary with keys from the provided iterable and values set to the supplied value.

main.py
my_dict = { 'name': 'Borislav Hadzhiev', 'site': 'bobbyhadz.com', 'id': 1, } # ✅ Reset all values in dictionary (keeping the keys) my_dict = dict.fromkeys(my_dict, None) print(my_dict) # 👉️ {'name': None, 'site': None, 'id': None} # ------------------------------------------------ # ✅ Remove all key-value pairs from a dictionary my_dict.clear() print(my_dict) # 👉️ {}

We used the dict.fromkeys() method to reset all values in a dictionary.

The dict.fromkeys method takes an iterable and a value and creates a new dictionary with keys from the iterable and values set to the provided value.

main.py
my_dict = { 'name': 'Borislav Hadzhiev', 'site': 'bobbyhadz.com', 'id': 1, } my_dict = dict.fromkeys(my_dict, None) print(my_dict) # 👉️ {'name': None, 'site': None, 'id': None} my_dict = dict.fromkeys(my_dict, 0) print(my_dict) # 👉️ {'name': 0, 'site': 0, 'id': 0}

If you'd rather not change the dictionary in place, assign the output of calling dict.fromkeys() to a new variable.

main.py
my_dict = { 'name': 'Borislav Hadzhiev', 'site': 'bobbyhadz.com', 'id': 1, } new_dict = dict.fromkeys(my_dict, '') print(new_dict) # 👉️ {'name': '', 'site': '', 'id': ''}

If you need to remove all key-value pairs from the dictionary, use the dict.clear() method.

main.py
my_dict = { 'name': 'Borislav Hadzhiev', 'site': 'bobbyhadz.com', 'id': 1, } my_dict.clear() print(my_dict) # 👉️ {}

You can also use a dict comprehension as an alternative to dict.fromkeys().

Reset all values in a Dictionary using dict comprehension #

Use a dict comprehension to reset all values in a dictionary, e.g. my_dict = {key: None for key in my_dict}. The dict comprehension iterates over the dictionary and sets the value of each key to the supplied value.

main.py
my_dict = { 'name': 'Borislav Hadzhiev', 'site': 'bobbyhadz.com', 'id': 1, } my_dict = {key: None for key in my_dict} print(my_dict) # 👉️ {'name': None, 'site': None, 'id': None}

Dict comprehensions are very similar to list comprehensions.

They perform some operation for every key-value pair in the dictionary or select a subset of key-value pairs that meet a condition.

On each iteration, we set the value of the key to None and return the result.

Alternatively, you can use a for loop.

Reset all values in a Dictionary using a for loop #

To reset all values in a dictionary:

  1. Use a for loop to iterate over a copy of the dictionary.
  2. Reset each key to an initial value.
  3. After the last iteration, all keys in the dictionary will be set to the supplied initial value.
main.py
my_dict = { 'name': 'Borislav Hadzhiev', 'site': 'bobbyhadz.com', 'id': 1, } for key in my_dict.copy(): my_dict[key] = '' print(my_dict) # 👉️ {'name': '', 'site': '', 'id': ''}

We used the dict.copy() method to create a shallow copy of the dictionary when iterating.

This is necessary because we aren't allowed to iterate over an object and modify it at the same time.

On each iteration, we set the value of the current key to an empty string.

If you'd rather not modify the original dictionary, declare a separate variable.

main.py
my_dict = { 'name': 'Borislav Hadzhiev', 'site': 'bobbyhadz.com', 'id': 1, } new_dict = {} for key in my_dict: new_dict[key] = None print(new_dict) # 👉️ {'name': None, 'site': None, 'id': None}

Instead of modifying the existing dictionary, we set all keys to None in a new dictionary.

We aren't modifying the dictionary we are iterating over, so we didn't have to use the dict.copy() method.

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.