0

I am really shocked, and could not think of the logic why do this happened. This is what I did:

>>> import random
>>> c = a
>>> a
[1, 2, 3, 4, 5]
>>> c
[1, 2, 3, 4, 5]
>>> random.shuffle(a)
>>> a
[5, 1, 3, 2, 4]
>>> c
[5, 1, 3, 2, 4]
>>> random.shuffle(c)
>>> c
[5, 4, 3, 2, 1]
>>> a
[5, 4, 3, 2, 1]
>>> 

Expected result is, the array 'a' is not the same as 'c'. Please enlighten me with the light of your knowledge for explaining why was the result the same as expected result as I am going mad.

dee cue
  • 983
  • 1
  • 7
  • 23
  • 1
    you do not create a copy of your list with `c=a`; you just get a new reference to the *same* list. if you wanted a copy, you could to this: `c=a[:]`. there is no reason to be shocked. – hiro protagonist May 02 '17 at 10:21
  • oh wow thank you so much, i wasn't able to comprehend what was going on, turned out i didn't type the syntax i am supposed to write – dee cue May 02 '17 at 10:42

1 Answers1

1

c and a are the same object. That's why changing either changes the other. If you want to copy the object, here's one way to do it:

a = c[:]

found here

Community
  • 1
  • 1