Jump to content

python list referencing?

Go to solution Solved by tarfeef101,

Yeah, you're thinking the right stuff. 

By default, this is a copy by reference. This means that finalList is appending tempList to itself, and if tempList changes, then since that is what's inside of finalList, it gets changed too.

Whereas in the second example, each time your outer loop is created, you assign a new tempList instead of modifying the old one. So all of those copies of it inside of finalList are not modified, since each time you're working on a new one.

This is hopefully a good guide on copying/passing by reference vs value (I didn't read it tbh, I just took computer science in university): https://medium.freecodecamp.org/understanding-by-reference-vs-by-value-d49139beb1c4

 

P.S. if you want to do stuff the way in the top for whatever reason, python has a method copy.deepcopy you can use to deep copy an object. If that interests you, you may want to google shallow vs deep copies as well. 

I just wanted to double check something about python referencing. I was trying to create a 2d list using a tempList and a finalList, but noticed that by doing tempList.clear(), it would delete all the contents in finalList as well. However, if I were to just do tempList = [], the contents would be saved in finalList. Does this happen because in the former example, the finalList is still referencing tempList, so all the contents are just linked up? Whereas in the latter example, tempList = [] is creating a new object, thus allowing for the old contents to be kept vs being destroyed? 

 

Code and outputs: 

tempList = []
finalList = []

for i in range(3):
    for j in range (4):
        tempList.append(j)
    finalList.append(tempList)
    tempList.clear()
    
print (finalList)

#Output: [[], [], []]
##############################################

finalList = []

for i in range(3):
    tempList = []
    for j in range (4):
        tempList.append(j)
    finalList.append(tempList)
    
print (finalList)

#Output: [[0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3]]

 

Link to comment
https://linustechtips.com/topic/1048432-python-list-referencing/
Share on other sites

Link to post
Share on other sites

Yeah, you're thinking the right stuff. 

By default, this is a copy by reference. This means that finalList is appending tempList to itself, and if tempList changes, then since that is what's inside of finalList, it gets changed too.

Whereas in the second example, each time your outer loop is created, you assign a new tempList instead of modifying the old one. So all of those copies of it inside of finalList are not modified, since each time you're working on a new one.

This is hopefully a good guide on copying/passing by reference vs value (I didn't read it tbh, I just took computer science in university): https://medium.freecodecamp.org/understanding-by-reference-vs-by-value-d49139beb1c4

 

P.S. if you want to do stuff the way in the top for whatever reason, python has a method copy.deepcopy you can use to deep copy an object. If that interests you, you may want to google shallow vs deep copies as well. 

Main Rig: R9 5950X @ PBO, RTX 3090, 64 GB DDR4 3666, InWin 101, Full Hardline Watercooling

Server: R7 1700X @ 4.0 GHz, GTX 1080 Ti, 32GB DDR4 3000, Cooler Master NR200P, Full Soft Watercooling

LAN Rig: R5 3600X @ PBO, RTX 2070, 32 GB DDR4 3200, Dan Case A4-SFV V4, 120mm AIO for the CPU

HTPC: i7-7700K @ 4.6 GHz, GTX 1050 Ti, 16 GB DDR4 3200, AliExpress K39, IS-47K Cooler

Router: R3 2200G @ stock, 4GB DDR4 2400, what are cases, stock cooler
 

I don't have a problem...

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×