Skip to content

Instantly share code, notes, and snippets.

@turing4ever
Created August 19, 2018 05:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save turing4ever/6afcbee4df955ee14ac1dac1eccc2a25 to your computer and use it in GitHub Desktop.
Save turing4ever/6afcbee4df955ee14ac1dac1eccc2a25 to your computer and use it in GitHub Desktop.
Duplicate a list in python
# if e is immutable
[e] * n
# [e] is mutable
list_of_list = [[e] for _ in range(n)]
# if you do [e] * n, you will get n references to the same [e]
foo = [[]] *4
foo[0].append('x')
[['x'], ['x'], ['x'], ['x']]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment