How can I add item in dictionary | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
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 Respostas
+ 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