0
a = [1,2,3,4,5]
b  = a
a.remove(a[0])
print(b)

The values of a have been stored into b before a.remove was called. I don't get how this works.

ScottC
  • 3,941
  • 1
  • 6
  • 20

1 Answers1

2

because the b list is just a reference to a list and not a copy by value. if you want to have a copy of a list, try this:

a = [1, 2, 3, 4, 5]
b = a[:]
a.remove(a[0])
print(b)
Majid
  • 638
  • 6
  • 21