[issue25681] Assignment of one element in nested list changes multiple elements

2015-11-20 Thread STINNER Victor
Changes by STINNER Victor : -- resolution: -> not a bug status: open -> closed ___ Python tracker ___ ___ Python-bugs-list mailing li

[issue25681] Assignment of one element in nested list changes multiple elements

2015-11-20 Thread Yan
Yan added the comment: Thanks for the quick response. It makes sense to me. I misunderstood the * operator. -- ___ Python tracker ___

[issue25681] Assignment of one element in nested list changes multiple elements

2015-11-20 Thread Emanuel Barry
Emanuel Barry added the comment: Another way to fix this would be the following: >>> l=[[''] * 2 for _ in range(3)] >>> l [['', ''], ['', ''], ['', '']] >>> l[0][1] = "A" >>> l [['', 'A'], ['', ''], ['', '']] The * operator on lists doesn't create new elements, it only adds new references to t

[issue25681] Assignment of one element in nested list changes multiple elements

2015-11-20 Thread Emanuel Barry
Emanuel Barry added the comment: Your list `l` actually holds only one list, except three times. When you change it, it's reflected in all the lists. It's the equivalent of the following: >>> x=['', ''] >>> l=[x, x, x] Makes a bit more sense this way? Either way, yes, this is intentional behav

[issue25681] Assignment of one element in nested list changes multiple elements

2015-11-20 Thread Yan
New submission from Yan: Is this the correct behavior? >>> l=[['']*2]*3 >>> b=[['', ''], ['', ''], ['', '']] >>> l == b True >>> l[0][1]='A' >>> b[0][1]='A' >>> l == b False >>> l [['', 'A'], ['', 'A'], ['', 'A']] >>> b [['', 'A'], ['', ''], ['', '']] -- messages: 254978 nosy: ydu prior