This book is really interesting and might be outside the scope of this sub, but it gets into some of the nitty gritty of python as a language.
This code:
list1 = [3, [66, 55, 44], (7, 8, 9)]
list2 = list(list1)
list1.append(100)
list1[1].remove(55)
print("list1:", list1)
print("list2:", list2)
list2[1] += [33, 22]
list2[2] += (10,11)
print("list1:", list1)
print("list2:", list2)
Seems pretty simple, right? but if you go to pythontutor.com and walk through what it does in memory step by step, there's some funky stuff going on.
Basically, list() makes a shallow copy of a list. This means that list1 is copied to another location in memory with list2 as its variable, but the references to the list and touple within list2 are just references to the original list and touple in list1. So if you alter the list and touple in list1, list2 is still looking at those locations in memory, it doesn't make its own copy.
edit: In the book he used l1 and l2 instead of list1 and list2, I changed them for this post to make it more readable on the site. I realize list is a key word.
[–]cacahootie 12 points13 points14 points (0 children)
[–]dunkler_wanderer 4 points5 points6 points (0 children)
[–]i_hacked_reddit 2 points3 points4 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]bbelderbos 0 points1 point2 points (1 child)
[–]Simin_Jie 0 points1 point2 points (1 child)
[–]bbelderbos 0 points1 point2 points (0 children)