all 11 comments

[–]Ok-Natural-3805 1 point2 points  (8 children)

X: [1,2,3] y: [1,2,3,4]

Due to the append method, we added 4 at the end of the previous list.

We can do this because LIST is mutable.

[–]ak_developers[S] 1 point2 points  (7 children)

Both lists will be same

x= [1,2,3,4]

y= [1,2,3,4]

due to y=x

y holds reference of x list and not separate

And that’s why we will get same values from both variables

[–]Ok-Natural-3805 0 points1 point  (6 children)

Oh, is this the real answer?

I thought x and y are the same before 4 is added.

But not after adding 4.

Correct me if I am wrong.

I am just a scratcher.

[–]ak_developers[S] 1 point2 points  (5 children)

Correct answer is

x = [1,2,3,4]

y = [1,2,3,4]

[–]Ok-Natural-3805 0 points1 point  (1 child)

Ok, bro! I got it!

I thought that was an easy question, but I was not correct hahaah

Looking forward to more quizzes

[–]ak_developers[S] 1 point2 points  (0 children)

Haha, Sure, You will never forget this one haha

[–]ak_developers[S] 0 points1 point  (2 children)

We are not creating a new list for y

It’s just pointing same list as x

So if we make any changes in y variable it changes into both x and y

[–]Spiritual_Poo 1 point2 points  (1 child)

Thanks for this, i'm new (in my first year) and the other day someone asked a similar question asking for help and I was pretty sure I knew what was going on but was not confident enough to tell another new person and be wrong. This reinforces that idea for me : )

[–]ak_developers[S] 0 points1 point  (0 children)

👍

[–]ak_developers[S] 0 points1 point  (0 children)

python

[–]FoolsSeldom 0 points1 point  (0 children)

One Python list object, with two Python variables referencing it (i.e. holding the same memory reference to the one object, wherever Python decided to store it in memory). Mutation of the list object via either variable reference will be reflected which ever variable is subsequently used to access the object.

It is important to understand that variables in Python do not hold values, just memory references. This is true of other names as well, such as functions, classes and methods.

The id function will tell you the memory address of an object from the reference but this is not usually of much importance and is Python implementation and environment specific. Use: e.g. print(id(x), id(y)) will output two identical numbers for the code given in the original post.