How to set all Dictionary values to 0 in Python

avatar
Borislav Hadzhiev

Last updated: Feb 21, 2023
3 min

banner

# Table of Contents

  1. Set all dictionary values to 0 in Python
  2. Set all dictionary values to 0 using a dict comprehension
  3. Set all dictionary values to 0 using a for loop

# Set all dictionary values to 0 in Python

Use the dict.fromkeys() method to set all dictionary values to 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 = { 'bobby': 1, 'hadz': 2, 'com': 3, } my_dict = dict.fromkeys(my_dict, 0) print(my_dict) # ๐Ÿ‘‰๏ธ {'bobby': 0, 'hadz': 0, 'com': 0}

set all dictionary values to 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 = { 'bobby': 1, 'hadz': 2, 'com': 3, } new_dict = dict.fromkeys(my_dict, 0) print(new_dict) # ๐Ÿ‘‰๏ธ {'bobby': 0, 'hadz': 0, 'com': 0}

# Set all dictionary values to 0 using a dict comprehension

Alternatively, you can use a dict comprehension.

main.py
my_dict = { 'bobby': 1, 'hadz': 2, 'com': 3, } my_dict = {key: 0 for key in my_dict} print(my_dict) # ๐Ÿ‘‰๏ธ {'bobby': 0, 'hadz': 0, 'com': 0}

set all dictionary values to 0 using dict comprehension

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

This is a three-step process:

  1. Use a for loop to iterate over 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 = { 'bobby': 1, 'hadz': 2, 'com': 3, } for key in my_dict: my_dict[key] = 0 print(my_dict) # ๐Ÿ‘‰๏ธ {'bobby': 0, 'hadz': 0, 'com': 0}

set all dictionary values to 0 using for loop

We used a for loop to iterate over the dictionary.

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 = { 'bobby': 1, 'hadz': 2, 'com': 3, } new_dict = {} for key in my_dict: new_dict[key] = 0 print(new_dict) # ๐Ÿ‘‰๏ธ {'bobby': 0, 'hadz': 0, 'com': 0}

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

I've also written an article on how to sum all values in a dictionary.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev