python list referencing?
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.

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 accountSign in
Already have an account? Sign in here.
Sign In Now