r/pythonhelp • u/nicobautista • Aug 27 '20
Why 2 identical arrays initialized differently give me 2 different results?
5
u/pdepmcp Aug 27 '20
Because they are NOT the same. The second one (array2) is logically a 2 dimensional 4x4 array of integers.
In the first one (array) you created a list of 4 integer (the inner part), then you repeated that list 4 times. Logically speaking you get a one dimensional list, containing 4 references to the SAME list of 4 integers. That means that when you change an element in the inner list, you get it changed in each row, because each row points to the same object (list).
If you change the first element of each row you'll get an apparently odder behavior, because you'll see only the last value you put, in each row, because you are overwriting the same object.
1
u/NamasteWager Aug 27 '20
I am not near a compiler and I am probably completely wrong on this, but could you try changing m in the second for loop to x? I know in other languages it falls out of scope but I can't remember how it works in python
2
u/WhenRedditFlies Aug 27 '20 edited Aug 27 '20
Ah! I think I can sort of answer this one.
This is all about mutable objects: python is a bit lazy when it can be, it saves memory by having things of the same value stored in the same place.
Eg>>> a = 5>>> b = a>>> id(a) == id (b) True
(id () is a function to tell you in essence where it is in the RAM, a and b with the same id means the same chunk of memory stores them)
(Alternatively)
>>> a is bTrue
This link between a and b stays until you assign one of them to something else, and python realises it has to stop being lazy at some point, and give them their own bits of RAM.
This shared RAM also applies to lists.
>>> lst1 = [1, 2]>>> lst2 = lst1>>> lst1 is lst2True>>> lst1.append(3)>>> lst1[1, 2, 3]>>> lst2 [1, 2, 3]
This connection is created when you initialise the list by duplicating the previous rows, the *4 means that the extra lists you now have share the same point(s) in RAM which means they are the same object. Therefore affecting the first item of the top row affects in the same way the first items in all the duplicated rows. This doesn't happen with the one on the right because it was all initialised separately, they all have their own bits of RAM.
2
u/WhenRedditFlies Aug 27 '20
A minor pedantic point, but Python uses lists, not arrays. The difference being you can extend lists due to the nature of the low level programming behind them, you can't extend arrays. They are just set size blocks of RAM you get to play with until you delete the array.
A bit more detail if you're interested: how lists work is they store "pairs" of values to each item. In this pair, you have the value in the list, and another value which tells Python where the next item-pair is stored (called a "pointer", a "pointer" stores a "reference"). This way you can chain infinitely (with physical limitations in your PC of course), to append a new item, just change where the pointer in the last item points to, and put some data there for the next item in the list. Alternatively to insert an item into the middle of the list, change where the pointer of the previous pair points to, it now points to your new data that you want to insert. Part of that new data has a pointer which points to the next item in the list.
That's not the sort of things Python uses, I've just described a linked list. whoops.https://stackoverflow.com/questions/3917574/how-is-pythons-list-implemented has more accurate information.
1
u/nicobautista Aug 27 '20
Im trying to initialize a 2D square array of size n with zeros and then replace only the first row with [1, 2, ..., n]. When I initialize the array using
array = [[0]*n]*n, the loop populates all the rows even though I assign it the row index 0. When I manually initialize the array by typing each zero, it does what I want it to do.