Skip to content

Instantly share code, notes, and snippets.

@leangaurav
Last active May 11, 2019 15:17
Show Gist options
  • Save leangaurav/90ca330cbe3ef872b04530e590a30067 to your computer and use it in GitHub Desktop.
Save leangaurav/90ca330cbe3ef872b04530e590a30067 to your computer and use it in GitHub Desktop.
l1 = [1,2,3]
l2 = list(l1)  # 1. call list constructor
l3 = l1[:]     # 2. copy all elements via slicing
l4 = l1.copy() # 3. call list copy function

l1[0] = 10
print(l1)
print(l2)
print(l3)
print(l4)

Output
[10, 2, 3]
[1, 2, 3]
[1, 2, 3]
[1, 2, 3]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment