Python Forum
An array "mystery": The same array, the same operations but different outcomes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
An array "mystery": The same array, the same operations but different outcomes
#1
Shocked 
I wrote the following code, where two identical arrays h and h1 are defined in different ways.
Then I produce the same operations on them and print the modified arrays. To my surprise, the outcome is different. The outcome for h1 is what I was expecting for both.

Please help me to understand why.

Here is the code:
    l1=3
    l2=3
    
    h=l1*[l2*[0]] 
    
    h1=[[0, 0, 0], [0, 0, 0], [0, 0, 0] ]

    print(h==h1)
    
    
#operations on array h   
    
    for i in range(3):
        for j in range(3):
            
                
            if i==0: 
                h[0][0]=1
                
                
            elif j==0: 
                h[i][j]=99
                
            else: pass
        
    print(h)
#IDENTICAL operations on array h1
    for i in range(3):
        for j in range(3):
            
                
            if i==0: 
                h1[0][0]=1
                
                
            elif j==0: 
                h1[i][j]=99
                
            else: pass
        
    print(h1)
    
#QUESTION: Why print(h) and print(h1) result in different arrays, when the initial
# arrays and the operations on them are the same ?

The outcome:

True
[[99, 0, 0], [99, 0, 0], [99, 0, 0]]
[[1, 0, 0], [99, 0, 0], [99, 0, 0]]
Reply
#2
The initial arrays are not the same. The first one creates a list, then puts several references to that list in another list. There's only two list objects here, the inside one and the outside one. If the inside list is modified, all three references to it are changed.

The second list creates 3 separate interior lists and puts them all in another list. There are four separate list objects that are independent. Changing one doesn't affect the others.

>>> h = 3 * [3*[0]]
>>> print([id(x) for x in h])
[4327610368, 4327610368, 4327610368]   # 3 references to the same object
>>> h1 = [[0,0,0], [0,0,0], [0,0,0]]
>>> print([id(x) for x in h1])
[4328654144, 4328656704, 4328678144]   # 3 different objects
mewss likes this post
Reply
#3
(Feb-17-2021, 06:20 PM)bowlofred Wrote: The initial arrays are not the same. The first one creates a list, then puts several references to that list in another list. There's only two list objects here, the inside one and the outside one. If the inside list is modified, all three references to it are changed.

The second list creates 3 separate interior lists and puts them all in another list. There are four separate list objects that are independent. Changing one doesn't affect the others.

>>> h = 3 * [3*[0]]
>>> print([id(x) for x in h])
[4327610368, 4327610368, 4327610368]   # 3 references to the same object
>>> h1 = [[0,0,0], [0,0,0], [0,0,0]]
>>> print([id(x) for x in h1])
[4328654144, 4328656704, 4328678144]   # 3 different objects
Reply
#4
I can see now. Thank you!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Elegant way to apply each element of an array to a dataframe? sawtooth500 3 114 4 hours ago
Last Post: deanhystad
  Concatenate array for 3D plotting armanditod 1 189 Mar-21-2024, 08:08 PM
Last Post: deanhystad
  Convert numpy array to image without loading it into RAM. DreamingInsanity 7 5,722 Feb-08-2024, 09:38 AM
Last Post: paul18fr
  How Write Part of a Binary Array? Assembler 1 305 Jan-14-2024, 11:35 PM
Last Post: Gribouillis
  Loop over an an array of array Chendipeter 1 523 Nov-28-2023, 06:37 PM
Last Post: deanhystad
  How to remove some elements from an array in python? gohanhango 9 979 Nov-28-2023, 08:35 AM
Last Post: Gribouillis
  IPython errors for numpy array min/max methods muelaner 1 506 Nov-04-2023, 09:22 PM
Last Post: snippsat
  Convert np Array A to networkx G IanAnderson 2 625 Jul-05-2023, 11:42 AM
Last Post: IanAnderson
  Help using a dynamic array excel formula with XLWings FXMonkey 2 1,206 Jun-06-2023, 09:46 PM
Last Post: FXMonkey
  [Newbie] Multiple Array azhuda 3 952 Jun-01-2023, 04:29 AM
Last Post: azhuda

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020