Skip to content

Instantly share code, notes, and snippets.

View philipkiely's full-sized avatar
⌨️
Writing programming tutorials

Philip Kiely philipkiely

⌨️
Writing programming tutorials
View GitHub Profile
@philipkiely
philipkiely / weirdpythonbehavior.py
Last active June 16, 2020 19:03
A quick explanation of a python bug to watch out for
# When working with nested lists created with []*x, watch out for this bug:
a = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
a[0][1] += 1
print(a)
# gives [[0, 1, 0], [0, 0, 0], [0, 0, 0]]
# totally straightforward! but …
a = [[0]*3]*3
a[0][1] += 1
print(a)
# gives [[0, 1, 0], [0, 1, 0], [0, 1, 0]]