0

When I do:

a = [7,3,4]
b = a
b[0] = 10

b[0] is of course set to 10, but a[0] is also set to 10. Why is this?

Yu Hao
  • 115,525
  • 42
  • 225
  • 281
mdegges
  • 955
  • 3
  • 18
  • 39
  • 1
    Useful related reading: http://stackoverflow.com/questions/17246693/what-exactly-is-the-difference-between-shallow-copy-deepcopy-and-normal-assignm – NightShadeQueen Aug 28 '15 at 01:29

1 Answers1

4
b = a

This makes b and a reference to the same list object. If you want b to reference to a new list object that is a copy of a, try:

b = a[:]
Yu Hao
  • 115,525
  • 42
  • 225
  • 281