0
How can I add item in dictionary
How can I add an item to a dictionary.l? The question is: To count a letter in a text. If the input is âwordâ , then the dictionary is {âwâ:1, âoâ:1,ârâ:1,âdâ:1} Can I use .append()? What is the syntext for this
4 RĂ©ponses
+ 4
no append() in dictionary
str = input()
di = {i:str.count(i) for i in str}
di.update({"add1":6}) #adding key-value to di
di["add2"] = 7 #2nd method of adding
print(di)
+ 3
OR You can do it like this
text = input()
d = {a:text.count(a) for a in text}
+ 1
Endalk wow, really concise
0
If you have access to google.com, try searching for this: "How can I add item in dictionary".