0

related to letter counter

dict={} for i in range(20): if i>0: dict['m']=1 print(dict) output: ['m':1] why it does not output 'm':1 20 times ??

20th May 2021, 4:51 PM
kushal
3 Answers
+ 1
Keys in a dictionary are unique, so there can only be one entry with key 'm'. First time 'm': 1 is added to the dictionary, then that entry is set to 1 19 times (no change)
20th May 2021, 4:56 PM
Benjamin JĂŒrgens
Benjamin JĂŒrgens - avatar
+ 1
Because keys are unqiue in dictionary
20th May 2021, 4:56 PM
Naveen Rathore
Naveen Rathore - avatar
0
kushal because a key in a dict is unique... to store the count of a letter in a dict use: if key in dict: dict[key] += 1 else: dict[key] = 1 so if key exist in dictionary value is increased by one, else it is initialized to one ^^
20th May 2021, 4:54 PM
visph
visph - avatar