-1

is this what it means that x and y are the same object not just the values that they are basically connected ? or I'm getting the 'is' statement wrong

>>> x=[2,8]
>>> y=x
>>> x.reverse()
>>> y
[8, 2]
>>> y is x
True
  • http://nedbatchelder.com/text/names1.html is a great pycon presentation (or just read it; the blog post contains everything in the video) that tells everything everyone absolutely needs to know about names in Python to really understand the language. – Wooble May 27 '16 at 12:18
  • @Wooble will do right away ,thanx for recommendation ^^ – Ahmed Elbarky May 27 '16 at 12:26

2 Answers2

1

You have it exactly right on all counts, they are the same object, and that is what is is meant to show. = does not make a copy. One way to copy the list is with y = list(x).

Alex Hall
  • 34,833
  • 5
  • 57
  • 89
0

That's right, they are exactly the same object - they a sharing the same memory address. If you modify one, you modify them both, like so:

x = [2, 8]
y = x
x.reverse()

print(y) # [8, 2]

If you don't want this you can make a copy using y = x[:].

Pep_8_Guardiola
  • 5,002
  • 1
  • 24
  • 35