r/Python • • Jun 09 '16

Scientific Python Cheat Sheet

https://ipgp.github.io/scientific_python_cheat_sheet/
476 Upvotes

62 comments sorted by

View all comments

5

u/datosh Jun 10 '16

Maybe add the [:] operator as a nice 'pythonic' way to copy a list. Example:

l = [1, 2, 3, 4, 5]
k = l
k[3] = 0
print(k[3] == l[3])   # True
print(k[3], l[3])       # 0 0

l = [1, 2, 3, 4, 5]
k = l[:]
k[3] = 0
print(k[3] == l[3])   # False
print(k[3], l[3])       # 0 4

1

u/Ogi010 Jun 10 '16

I like the .copy method (only works in Python 3)

k = l.copy()