Open In App

Initialize Dictionary Python with 0

Last Updated : 19 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

This article will show how we can initialize a Dictionary in Python with 0 values. We will see various methods to initialize the value as 0. Below is an example for understanding the problem statement.

Example:

In the below example, we have an input dictionary with keys and values. The values are non-zero which we will initialize to 0 using different approaches or methods.

Input = {'Algorithms': 10, 'Data Structures': 15, 'Python': 8, 'Machine Learning': 5} 
Output = {'Algorithms': 0, 'Data Structures': 0, 'Python': 0, 'Machine Learning': 0}

Initialize Dictionary Python with 0

There, are different methods to initialize a dictionary in Python with the 0 value. Below we are explaining all the possible approaches with practical implementation.

  • Using Assignment
  • Using a For Loop
  • Using Dictionary Comprehension
  • Using dict.fromkeys() Method
  • Using defaultdict (Collections Module)

Dictionary Initialize with Zero Value

Here, we initialize the dictionary with direct zero values and then print using the print function.

Python3




# Input dictionary with zero values
input_dict = {'Algorithms': 0, 'Data Structures': 0, 'Python': 0, 'Machine Learning': 0}
     
#Print Output
print(input_dict)


Output :

{'Algorithms': 0, 'Data Structures': 0, 'Python': 0, 'Machine Learning': 0}

Set all Dictionary Values to 0 using a For Loop

In this example, we use the loop to initialize the dictionary with 0 values. Initially, we took the input_dict with non-zero values and then we copied the input_dict dictionary and reset all values to 0 using a For loop. After resetting the values, we store them in the output_dict object and print them as output.

Python3




# Input dictionary with non-zero values
input_dict = {'Algorithms': 10, 'Data Structures': 15, 'Python': 8, 'Machine Learning': 5}
 
# Setting initial values
output_dict = input_dict.copy()
 
# Resetting values to 0
for key in output_dict:
    output_dict[key] = 0
     
#Print Output
print(output_dict)


Output

{'Algorithms': 0, 'Data Structures': 0, 'Python': 0, 'Machine Learning': 0}

 Set all dictionary values to 0 Using Dictionary Comprehension

In this example, we are using the Dictionary Comprehension, where we have taken the input_dict with non-zero values, later we have created output_dict on which the Dictionary Comprehension is applied that resets the non-zero values to the 0 values.

Python3




# Input dictionary
input_dict = {'Algorithms': 10, 'Data Structures': 15, 'Python': 8, 'Machine Learning': 5}
 
# Setting initial values
output_dict = input_dict.copy()
 
# Resetting values to 0 using comprehension
output_dict = {key: 0 for key in output_dict}
 
# Print Output
print(output_dict)


Output

{'Algorithms': 0, 'Data Structures': 0, 'Python': 0, 'Machine Learning': 0}

Initialize Dictionary Python Using dict.fromkeys() Method

In this example, we are using the dict.fromkeys() method to reset all the values to 0. The dict.fromkeys() method is used to create a new dictionary with specified keys, each initialized to a default value. This method created an output_dict dictionary and initialized all values to 0.

Python3




# Create a dictionary
input_data = {'Algorithms': 10, 'Data Structures': 15, 'Python': 8, 'Machine Learning': 5}
 
# Setting initial values
output_dict = input_data .copy()
 
# Resetting values to 0 using fromKeys method
output_dict.update(dict.fromkeys(output_dict, 0))
 
#Print output
print(output_dict)


Output

{'Algorithms': 0, 'Data Structures': 0, 'Python': 0, 'Machine Learning': 0}

Initialize Dictionary Python Using defaultdict (Collections Module)

In this example, we are using defaultdict from the Collections Module to initialize a dictionary output_dict with default values of 0 for unspecified keys. USing the update method the value is been reset from non-zero to 0 and printed using the print method in Python.

Python3




# Importing defaultdict
from collections import defaultdict
 
# Input dictionary
input_data = {'Algorithms': 10, 'Data Structures': 15, 'Python': 8, 'Machine Learning': 5}
 
# Setting initial values
output_dict = defaultdict(int, input_data)
 
# Resetting values to 0 using defaultdict
output_dict.update({key: 0 for key in output_dict})
 
# Print Output
print(dict(output_dict))


Output

{'Algorithms': 0, 'Data Structures': 0, 'Python': 0, 'Machine Learning': 0}

Conclusion

In conclusion, initializing a dictionary in Python with 0 as its default value provides a convenient and efficient way to manage key-value pairs, especially when dealing with numeric data or counting occurrences. By setting the default value to 0, you create a foundation for incrementing values based on keys without the need for explicit checks or error handling.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads