Consider this code:
lst = [['a']*5]*4
lst[1][1] = '*'
And this code:
lst= [['a'] * 5 for i in range(4)]
lst[1][1] = '*'
What makes them different?
Thanks
Consider this code:
lst = [['a']*5]*4
lst[1][1] = '*'
And this code:
lst= [['a'] * 5 for i in range(4)]
lst[1][1] = '*'
What makes them different?
Thanks
Those are the same objects:
>>> lst = [['a']*2]*2
>>> map(id, lst)
[60543624, 60543624]
>>> lst[0] is lst[1]
True
Because, you are not creating four lists with 5 a
s in them. Instead, you are creating 4 lists which point to the same list with 5 a
s in it. So, you change one, the effect is seen in all others as well.