Dictionary adds an unexpected list - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Dictionary adds an unexpected list (/thread-16782.html) |
Dictionary adds an unexpected list - erina - Mar-14-2019 Hello, my expected output is : data={'Switchboard1':[{'Light':[1,2,3,4],'Fan':[1,2,3,4]}]} But when the code executes the underlined point it adds the key "switchboard" to dic1,eventhough I initialize dic1 only with "delta". delta=[1,2,3,4] dic1=dic2={} data={'Switchboard1':['Light','Fan']} home= data.keys() for k, v in data.iteritems(): for i in v: dic1[i]=delta print dic1,k [u]dic2[k]=dic1[/u] print dic1 print dic2 dic1=dic2={} RE: Dictionary adds an unexpected list - ichabod801 - Mar-14-2019 Because of line 2, dic1 and dic2 are the same dict. Any changes you make to one are made to the other. Use this instead: dic1, dic2 = {}, {} |