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
#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


Messages In This Thread
RE: An array "mystery": The same array, the same operations but different outcomes - by bowlofred - Feb-17-2021, 06:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Elegant way to apply each element of an array to a dataframe? sawtooth500 7 479 Mar-29-2024, 05:51 PM
Last Post: deanhystad
  Concatenate array for 3D plotting armanditod 1 312 Mar-21-2024, 08:08 PM
Last Post: deanhystad
  Convert numpy array to image without loading it into RAM. DreamingInsanity 7 5,953 Feb-08-2024, 09:38 AM
Last Post: paul18fr
  How Write Part of a Binary Array? Assembler 1 406 Jan-14-2024, 11:35 PM
Last Post: Gribouillis
  Loop over an an array of array Chendipeter 1 611 Nov-28-2023, 06:37 PM
Last Post: deanhystad
  How to remove some elements from an array in python? gohanhango 9 1,332 Nov-28-2023, 08:35 AM
Last Post: Gribouillis
  IPython errors for numpy array min/max methods muelaner 1 607 Nov-04-2023, 09:22 PM
Last Post: snippsat
  Convert np Array A to networkx G IanAnderson 2 715 Jul-05-2023, 11:42 AM
Last Post: IanAnderson
  Help using a dynamic array excel formula with XLWings FXMonkey 2 1,341 Jun-06-2023, 09:46 PM
Last Post: FXMonkey
  [Newbie] Multiple Array azhuda 3 1,057 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