How to set all dictionary key values to 0 in Python

2 Answers

0 votes
dic = {'a':13, 'c':8, 'd':24, 'e':36, 'f':17, 'g':6, 'h':25}
  
dic = dict.fromkeys(dic, 0)
 
print(dic)
  
  
  
  
'''
run:

{'a': 0, 'c': 0, 'd': 0, 'e': 0, 'f': 0, 'g': 0, 'h': 0}

'''

 



answered Mar 15, 2023 by avibootz
0 votes
dic = {'a':13, 'c':8, 'd':24, 'e':36, 'f':17, 'g':6, 'h':25}
  
for key in dic:
    dic[key] = 0
 
print(dic)
  
  
  
  
'''
run:

{'a': 0, 'c': 0, 'd': 0, 'e': 0, 'f': 0, 'g': 0, 'h': 0}

'''

 



answered Mar 15, 2023 by avibootz
...