I’m not even able to properly search google for it, but here goes:
a = {}
b = {}
c = [a, b]
for d in c:
d['ID'] = d
print c
returns:
[{'ID': {...}}, {'ID': {...}}]
why isn’t it:
[{'ID': a}, {'ID': b}]
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Let’s step through this:
So far, so good.
We can unroll this to:
And expand that to:
Now substitute:
So, let’s forget about the loop for a second and look at what that does:
In other words, you’re making each
dictrecursively contain a copy of itself, under the keyID. How would you expect this to be printed?So, the obvious thing to do is to try to print the whole dictionary:
But this would be an infinitely-long string, and Python would run out of stack space before reaching infinity. So it needs to truncate it somehow.
It can’t print this:
Because
ais just a name that happens to be bound to thedict, just likedis at the time. In fact, the loop doesn’t even know thatais bound to that at the time; it knows thatdis. But even if it did know, the result would be wrong. Think about this:So, the obvious answer is to use an ellipsis (kind of like I did in the human-readable version) to represent “and so on”.