Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python aliasing [duplicate]

I understand that given this code

a = [1,2,3]
b = [4,5,6]
c = a

and then by doing this

a[0] = 0

I wil change first positions of both a and c. Could somebody explain why this doesn't apply when I do this:

a = b

ie. why c doesn't become equal to b?

like image 314
kubajed Avatar asked Nov 22 '25 05:11

kubajed


1 Answers

 a = [1,2,3]
 b = [4,5,6]

 #       a  ────────>    [1,2,3]
 #       b  ────────>    [4,5,6]


 c = a    # Changing 'c' to point at the list that 'a' points at

 #       c  ─────┐
 #       a  ─────┴──>    [1,2,3]
 #       b  ────────>    [4,5,6]


 a = b    # Changing 'a' to point at the list that 'b' points at

 #       c  ─────┐
 #       a  ──┐  └──>    [1,2,3]
 #       b  ──┴─────>    [4,5,6]
like image 167
Aziz Avatar answered Nov 23 '25 17:11

Aziz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!