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

18th May 2021, 9:02 AM
Weitao Sun
Weitao Sun - avatar
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)
18th May 2021, 9:17 AM
TOLUENE
TOLUENE - avatar
+ 3
OR You can do it like this text = input() d = {a:text.count(a) for a in text}
18th May 2021, 9:35 AM
Endalk
Endalk - avatar
+ 1
Endalk wow, really concise
18th May 2021, 10:02 AM
Weitao Sun
Weitao Sun - avatar
0
If you have access to google.com, try searching for this: "How can I add item in dictionary".
19th May 2021, 4:41 AM
NEZ
NEZ - avatar