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.