Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

214
Views
What happens in memory while unpacking a collection?

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

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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]".

over 4 years ago · Santiago Trujillo Report

0

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)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!