Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Dictionary adds an unexpected list
#1
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={}
Quote
#2
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 = {}, {}
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures

Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  update values in list based on dictionary bunti 3 631 Jun-10-2019, 07:26 AM
Last Post: perfringo
  Search a List of Dictionaries by Key-Value Pair; Return Dictionary/ies Containing KV dn237 19 401 May-29-2019, 02:27 AM
Last Post: heiner55
  Iterate through a list of dictionary and append a new value. erina 1 158 May-16-2019, 09:55 AM
Last Post: perfringo
  Convert List of Dictionary to dictionary of dictionary list in python kk230689 2 191 Apr-27-2019, 03:13 AM
Last Post: perfringo
  syntax error near unexpected token btom529 5 267 Apr-21-2019, 09:03 PM
Last Post: btom529
  Getting an unexpected generator object ? mcgrim 19 424 Mar-29-2019, 11:35 AM
Last Post: gontajones
  unexpected EOF while parsing whatloop 3 412 Mar-09-2019, 06:59 PM
Last Post: whatloop
  Unexpected round behavior pythonCoder 1 162 Feb-19-2019, 02:39 PM
Last Post: marienbad
  Unexpected expected type error result MartinMaker 1 199 Feb-16-2019, 05:02 PM
Last Post: micseydel
  Unexpected ininite loop behavior RedSkeleton007 4 207 Jan-27-2019, 05:32 PM
Last Post: aakashjha001

Forum Jump:


Users browsing this thread: 1 Guest(s)