3

Say

list1=[4,8,12]

a,b,c=list1

output is a=4,b=8,c=12.

My confusion

Instructor told us that it is not like a gets mapped to 4, b to 8, and c to 12. I didn't understand what he told clearly (although I listened repeatedly to him multiple times). He was telling something like object is created for 4 and a is mapped to 4. But what is the difference between this and what I have presented below in figure?

handwritten diagram

martineau
  • 119,623
  • 25
  • 170
  • 301
ladhee
  • 71
  • 5
  • 2
    Since we don't know what exactly your instructor said to you we can't tell you what the difference is to what you have shown. What you have shown makes sense anyway. – mkrieger1 Nov 24 '21 at 22:13
  • 2
    Maybe he meant that for example the "4" object is stored somewhere (not directly inside the list) and both `list1[0]` and `a` are referring to it. – mkrieger1 Nov 24 '21 at 22:16
  • Does [Unpacking, extended unpacking and nested extended unpacking](https://stackoverflow.com/questions/6967632/unpacking-extended-unpacking-and-nested-extended-unpacking) answer your question? – Sayse Nov 24 '21 at 22:16
  • 1
    Also see [Facts and myths about Python names and values](https://nedbatchelder.com/text/names.html) which explains what names like `list1`, `a`, etc actually are. – martineau Nov 24 '21 at 22:37

2 Answers2

6

The thing about your picture that's misleading is that it implies that a, b, and c reference slices of list1. If you change list1, though, you will find that a, b, and c aren't affected by that change.

A better way to draw the picture might be to show 4, 8, and 12 separate from list1:

list1-->[ ][ ][ ]
         |  |  |
         V  V  V
         4  8  12
         ^  ^  ^
         |  |  |
         a  b  c

All of the variables are independent of one another, even though some of them (e.g. list1[0] and a) currently point to the same values.

To put it another way: saying a = list1[0] is saying "evaluate list1[0] and assign a to reference whatever that value is right now", which is not the same as saying "make a be an alias for list1[0]".

Samwise
  • 68,105
  • 3
  • 30
  • 44
1

Try this:

# define the list
list1=[4,8,12]

# reserve 3 memory spaces and unpack the values from list into them
# those memory spaces will contain one integer each one of the size of
# sys.getsizeof(int()) == 28 bytes (Python 3)
# a,b and c are actually pointers to those memory spaces
a,b,c=list1

print(a,b,c)

# change the first value of the list
list1[0] = 56
print(list1)
    
# now check that indeed "a" is not the same pointer than "list1[0]"
print(a)

But

You must to be careful with this kind of asignations with lists, try also this:

list2 = list1
print(list1, list2)

# then change any of them
list1 [0] = -1

# check that "list2" is pointing to the same memory address than "list1"
print(list1, list2)
emichester
  • 189
  • 9
  • 1
    I wish I could give 2 right answers, your answer helped me understand that second part. (list1=list2)Thank you bro. – ladhee Nov 25 '21 at 07:11
  • Hey! One thing I forgot to say, for almost all the conceptual tests (like this one) I use "https://colab.research.google.com/" which is free, online and they provides you with a good processor and RAM (also GPU and TPU if needed). This plattform is like Jupyter Notebooks but better. – emichester Dec 06 '21 at 13:21