+ 1

How to add an element to dictionary?

except this form a={'c':2} a ['b']=3 is there any other form to add?

20th Jan 2018, 10:59 AM
Initiator
5 Answers
+ 1
https://stackoverflow.com/q/1024847/8458437 There appears to be an update method on dictionaries
20th Jan 2018, 2:32 PM
Dan Walker
Dan Walker - avatar
+ 1
Yea ThankušŸ˜Š..but I don't wanna update as it inserts a new value for the same key, if the key already exists. I wanted to insert another key value pair even if the key has already occurred. hope u got my pointšŸ˜…
20th Jan 2018, 2:39 PM
Initiator
+ 1
hm not sure that's possible, the point of a dictionary is having a unique key to map to a value, but values need not be unique. Is your dictionary set up correctly?
20th Jan 2018, 2:41 PM
Dan Walker
Dan Walker - avatar
+ 1
Actually now I have modified my code a bit sent=[['this','is','beautiful','and','nice','place'],['nice','place']] adj=['beautiful','nice'] freq={} a=1 for c in sent: for x in c: if x in adj: freq [a]={} freq [a][x]=c.count (x) a+=1 print (freq) the output I'm getting is {1:{'nice':1},2:{'nice':1}} bt i need the following output, {1:{'beautiful':1},{'nice':1},2:{'nice':1}}
20th Jan 2018, 2:54 PM
Initiator
0
not that i know off
20th Jan 2018, 12:47 PM
Julian
Julian - avatar