Set all dictionary values to 0 in Python

avatar

Borislav Hadzhiev

Last updated: Sep 17, 2022

banner

Photo from Unsplash

Set all dictionary values to 0 in Python #

Use the dict.fromkeys() method to set all dictionary values to 0, e.g. my_dict = dict.fromkeys(my_dict, 0). 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 = { 'first': 1, 'second': 2, 'third': 3, } my_dict = dict.fromkeys(my_dict, 0) print(my_dict) # 👉️ {'first': 0, 'second': 0, 'third': 0}

We used the dict.fromkeys() method to set all dictionary values to zero.

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
# 👇️ {'a': None, 'b': None, 'c': None} print(dict.fromkeys({'a': 'bobby', 'b': 'hadz', 'c': 'com'})) # 👇️ {'a': 0, 'b': 0, 'c': 0} print(dict.fromkeys({'a': 'bobby', 'b': 'hadz', 'c': 'com'}, 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 = { 'first': 1, 'second': 2, 'third': 3, } new_dict = dict.fromkeys(my_dict, 0) print(new_dict) # 👉️ {'first': 0, 'second': 0, 'third': 0}

Alternatively, you can use a dict comprehension.

Set all dictionary values to 0 using a dict comprehension #

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

main.py
my_dict = { 'first': 1, 'second': 2, 'third': 3, } my_dict = {key: 0 for key in my_dict} print(my_dict) # 👉️ {'first': 0, 'second': 0, 'third': 0}

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 zero and return the result.

Alternatively, you can use a simple for loop.

Set all dictionary values to 0 using a for loop #

To set all values in a dictionary to 0:

  1. Use a for loop to iterate over a copy of the dictionary.
  2. Set the value of each key to zero.
  3. After the last iteration, all of the values in the dictionary will be set to 0.
main.py
my_dict = { 'first': 1, 'second': 2, 'third': 3, } for key in my_dict.copy(): my_dict[key] = 0 print(my_dict) # 👉️ {'first': 0, 'second': 0, 'third': 0}

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 0.

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

main.py
my_dict = { 'first': 1, 'second': 2, 'third': 3, } new_dict = {} for key in my_dict: new_dict[key] = 0 print(new_dict) # 👉️ {'first': 0, 'second': 0, 'third': 0}

Instead of modifying the existing dictionary, we set all keys to 0 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.