Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Sequences Sequence Operations Concatenation and Multiplication

Concatenation and Memory Storage With Mutable and Immutable Objects (Mainly Tuples and Lists)

Hello,

I am building upon another thread that asked a similar question, however I feel the need to build off that in order to build my domain knowledge on this topic.

When creating a variable that is a tuple and concatenating with another tuple but storing that result in the same variable name prior to concatenation the object id types are different. This is expected since tuples are immutable and the original object is disposed of while the variable name is now referencing our new object. (Please see code snippet below)

obj1 = 1,2,3,4,5
print(id(obj1))
obj2= 6,7,8,9,10
obj1 = obj1+obj2
print(obj1)
print(id(obj1)) #The id of this object is not the same as when we printed it earlier (As expected)

However, when we follow the same process for lists the id of the object prior to concatenation and after is actually different. This is where I'm confused and please correct me If I am not thinking of this properly. However, since lists are mutable shouldn't concatenation modify the original object so in this case there is no actual disposal of the object prior to concatenation (and therefore no variable reassignment occurred). Therefore, shouldn't the object id's be the same?

list1 = [1,2,3,4]
list2 = [1,2,3,4]
print(id(list1))
list1 = list1 + list2
print(id(list1))

1 Answer

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 68,423 Points

Hey heebers, great question!

The right hand equation list1 + list2 creates a new list each time it evaluates. It is this new list that gets a new id and then is pointed to by list1.

>> list1 = [4, 5, 6]
>>> list2 = [3, 2, 1]
>>> id(list1), id(list2)
(140148603620800, 140148602798720)
>>> list1 + list2
[4, 5, 6, 3, 2, 1]
>>> id(list1 + list2)
140148602799168
>>> id(list1 + list2)
140148602798656
>>> list1 = list1 + list2
>>> id(list1)
140148602920896

Depending on the freed memory space, the newly created list may or may not happen to use the same memory location as a previous object. Above, the temp object is in two different locations. But since the object pointed to by list1 is "in use" when the new temp list is created, the temp list location will always be different then the original list1. Thus, the new list1 will be located a the new address.

Edit: if the code was list1 += list2 then the list1 id would remain the same since this translates to an append operation on the original object.

Post back if you need more help. Good luck!!

Yes, thank you so much. I appreciate you taking the time to do this I was really caught up on this one.

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

Interestingly, if you use the incremental add += then the ids remain unchanged:

>>> lst1 = [4, 5, 6]
>>> lst2 = [0, 1, 2]
>>> id(lst1), id(lst2)
(4583466888, 4585612040)
>>> lst1 += lst2
>>> lst1
[4, 5, 6, 0, 1, 2]
>>> id(lst1), id(lst2)
(4583466888, 4585612040)

Using += calls lst1._iadd_(lst2) which preserves the id.

When using the += to increment is the ID preserved because python assumes we would not be changing the variable when incrementing it at the same time?

Chris Freeman
Chris Freeman
Treehouse Moderator 68,423 Points

+= calls the __iadd__ method of the lst1 that modifies self in place. No new object is created.

Oh thatโ€™s pretty neat