Got it My confusion was why the three equal elements
Now i see it clearly If i add the temp1 3 times to temp, when i change it for the last time inside the loop it changes all the 3 elements inside the full list temp. Thank you 2015-04-01 14:21 GMT+01:00 Anthony <[email protected]>: > In the first example, you have only a single dictionary (created outside > the loop). You then append that single dictionary to a list multiple times. > Each item in the list is still just that single dictionary (you are not > creating separate copies of the dictionary). In the first loop, you are > setting all the values to 0, so when you print, you see 0's. In the next > loop, you set all the values to 1, so you then see 1's. But in the final > loop, you set all the values to 2. So, when you finally print the full list > (after the for loop), you are just printing the same dictionary of 2's > three times. > > In the second example, you create a completely new dictionary within each > iteration of the loop (which you happen to assign to the same variable > name). In this case, you are building a list with three separate > dictionaries, each of which contains different values. > > In both examples, replace the final line of the loop with: > > temp.append(id(temp1)) > > You will see in the first case that you get the same id three times > (indicating it is the same dictionary object), but in the second case, you > will get three different ids (indicating three different dictionary > objects). > > Anthony > > > On Wednesday, April 1, 2015 at 8:34:56 AM UTC-4, Ramos wrote: >> >> The 2nd example is bullet proof. >> >> However the 1st example should work also because if i print temp1 before >> append temp1 to the temp var i see it like >> >> {'a': 0, 'c': 0, 'b': 0} >> {'a': 1, 'c': 1, 'b': 1} >> {'a': 2, 'c': 2, 'b': 2} >> >> temp=[] >> temp1={'a':0,'b':0,'c':0} >> for x in range(3): >> temp1['a']=x >> temp1['b']=x >> temp1['c']=x >> *print temp1* >> temp.append(temp1) >> print temp >> >> Still dont get it >> >> 2015-04-01 12:47 GMT+01:00 Leonel Câmara <[email protected]>: >> >>> The difference is that in the second example you're creating the >>> dictionary inside the loop. In the first example you create the dictionary >>> outside so each step of the loop is modifying the same dictionary and then >>> you put that same dictionary 3 times in the list. >>> >>> -- >>> Resources: >>> - http://web2py.com >>> - http://web2py.com/book (Documentation) >>> - http://github.com/web2py/web2py (Source code) >>> - https://code.google.com/p/web2py/issues/list (Report Issues) >>> --- >>> You received this message because you are subscribed to the Google >>> Groups "web2py-users" group. >>> To unsubscribe from this group and stop receiving emails from it, send >>> an email to [email protected]. >>> For more options, visit https://groups.google.com/d/optout. >>> >> >> -- > Resources: > - http://web2py.com > - http://web2py.com/book (Documentation) > - http://github.com/web2py/web2py (Source code) > - https://code.google.com/p/web2py/issues/list (Report Issues) > --- > You received this message because you are subscribed to the Google Groups > "web2py-users" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/d/optout. > -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.

