KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I've encountered what I think is a strange behavior in Python, and I'd like somebody to explain it if possible. I've created an empty 2D list listy = [[]]*3 print listy [[], [], []] The following works as I'd expect: listy[1] = [1,2] yields [[], [1,2], []] listy[1].append(3) yields [[], [1,2,3], []] However, when I append to one of the empty lists, python appends to ALL of the sublists, as follows: listy[2].append(1) yields [[1], [1,2,3], [1]] . Can anyone explain to me why this behavior occurs?
Tags (comma-separated)
Save Edits
Cancel