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 Antworten
+ 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".