The problem

You're appending the same array a to your list0 4 times. Arrays like a are mutable objects, which means, among other things, that when you assign values to them the underlying object changes. Since the array is present in your list 4 times, those changes (seem to) show up in 4 different places.

Solution

You can fix the code you have with one small change. Append a copy of the array to your list, instead of the array itself:

import numpy as np
a = np.empty((3), int)
list0 = []
for idx in range(4):    
    for i in range(3):
        a[i] = idx*10 + i
    print("idx =",idx,"; a =",a)
    list0.append(a.copy())
print("list0 =",list0)

Output:

idx = 0 ; a = [0 1 2]
idx = 1 ; a = [10 11 12]
idx = 2 ; a = [20 21 22]
idx = 3 ; a = [30 31 32]
list0 = [array([0, 1, 2]), array([10, 11, 12]), array([20, 21, 22]), array([30, 31, 32])]

Optimized solution

Python/Numpy offer many better ways (both in terms of using fewer lines of code and running faster) to initialize arrays. For a bunch of ranges like this, here is a reasonable approach:

list0 = [np.arange(n*10, n*10+3) for n in range(4)]
print(list0)

Output:

[array([0, 1, 2]), array([10, 11, 12]), array([20, 21, 22]), array([30, 31, 32])]

You might also consider just using a single 2D array in place of a list of arrays. One single array is often easier to work with than a heterogenous mix of arrays in a list. Here's how you do that:

arr0 = np.array([np.arange(n*10, n*10+3) for n in range(4)])
print(arr0)

Output:

[[ 0  1  2]
 [10 11 12]
 [20 21 22]
 [30 31 32]]
Answer from tel on Stack Overflow
Top answer
1 of 2
14

The problem

You're appending the same array a to your list0 4 times. Arrays like a are mutable objects, which means, among other things, that when you assign values to them the underlying object changes. Since the array is present in your list 4 times, those changes (seem to) show up in 4 different places.

Solution

You can fix the code you have with one small change. Append a copy of the array to your list, instead of the array itself:

import numpy as np
a = np.empty((3), int)
list0 = []
for idx in range(4):    
    for i in range(3):
        a[i] = idx*10 + i
    print("idx =",idx,"; a =",a)
    list0.append(a.copy())
print("list0 =",list0)

Output:

idx = 0 ; a = [0 1 2]
idx = 1 ; a = [10 11 12]
idx = 2 ; a = [20 21 22]
idx = 3 ; a = [30 31 32]
list0 = [array([0, 1, 2]), array([10, 11, 12]), array([20, 21, 22]), array([30, 31, 32])]

Optimized solution

Python/Numpy offer many better ways (both in terms of using fewer lines of code and running faster) to initialize arrays. For a bunch of ranges like this, here is a reasonable approach:

list0 = [np.arange(n*10, n*10+3) for n in range(4)]
print(list0)

Output:

[array([0, 1, 2]), array([10, 11, 12]), array([20, 21, 22]), array([30, 31, 32])]

You might also consider just using a single 2D array in place of a list of arrays. One single array is often easier to work with than a heterogenous mix of arrays in a list. Here's how you do that:

arr0 = np.array([np.arange(n*10, n*10+3) for n in range(4)])
print(arr0)

Output:

[[ 0  1  2]
 [10 11 12]
 [20 21 22]
 [30 31 32]]
2 of 2
7

Just do this:

list_to_append.append(np_array.copy())

In a nutshell, numpy arrays or lists are mutable objects, which means that you when you assign a numpy array or list to a variable, what you are really assigning are references to memory locations aka pointers.

In your case, "a" is a pointer, so what you are really doing is appending to list0 an address to the memory location pointed by "a", and not the actual values pointed by the pointer. Thus it means that each new position of "list0", after appending, turns out to be the same address of memory: "a".

So, instead of:

list0.append(a)

You call the copy() method of "a" that creates a new memory location for the new values of "a" and returns it:

list0.append(a.copy())
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ append-in-python-how-to-append-to-a-list-or-an-array
Append in Python โ€“ How to Append to a List or an Array
January 7, 2022 - If you take a closer look at the output from above, ['JavaScript', 'Java', ['Python', 'C++']], you'll see that a new list got added to the end of the already existing list. So, .append() adds a list inside of a list.
๐ŸŒ
Real Python
realpython.com โ€บ python-append
Python's .append(): Add Items to Your Lists in Place โ€“ Real Python
October 21, 2023 - This method works similarly to its list counterpart, adding a single value to the end of the underlying array. However, the value must have a data type thatโ€™s compatible with the existing values in the array. Otherwise, youโ€™ll get a TypeError. For example, if you have an array with integer numbers, then you canโ€™t use .append() to add a floating-point number to that array:
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ ref_list_append.asp
Python List append() Method
Remove List Duplicates Reverse ... Q&A Python Bootcamp Python Certificate Python Training ... The append() method appends an element to the end of the list....
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ python-add-to-array
Python Array Add: How to Append, Extend & Insert Elements | DigitalOcean
April 15, 2025 - Note: You can only add elements of the same data type to an array. Similarly, you can only join two arrays of the same data type. With the array module, you can concatenate, or join, arrays using the + operator and you can add elements to an array using the append(), extend(), and insert() methods.
๐ŸŒ
AskPython
askpython.com โ€บ home โ€บ how to append an array in python?
How to append an Array in Python? - AskPython
January 16, 2024 - ... The list or the element gets added to the end of the list and the list is updated with the added element. ... We can create an array using the Array module and then apply the append() function to add elements to it.
๐ŸŒ
w3resource
w3resource.com โ€บ python-exercises โ€บ numpy โ€บ append-a-numpy-array-to-a-python-list-and-print-the-result.php
Append a NumPy array to a Python list and print the result
Create NumPy Array: Define a NumPy array with some example data. Append Array to List: Convert the NumPy array to a list using the tolist() method and append it to the Python list using the + operator.
Find elsewhere
๐ŸŒ
Python documentation
docs.python.org โ€บ 3 โ€บ tutorial โ€บ datastructures.html
5. Data Structures โ€” Python 3.14.3 documentation
The list methods make it very easy to use a list as a stack, where the last element added is the first element retrieved (โ€œlast-in, first-outโ€). To add an item to the top of the stack, use append(). To retrieve an item from the top of the ...
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_lists_add.asp
Python - Add List Items
Note: As a result of the examples above, the lists will now contain 4 items. To append elements from another list to the current list, use the extend() method.
๐ŸŒ
PhoenixNAP
phoenixnap.com โ€บ home โ€บ kb โ€บ devops and development โ€บ add elements to python array
Add Elements to Python Array (3 Methods)
December 15, 2025 - 3. Add one or more elements to an array end with the extend() method. Provide the elements in an iterable data type. The items in the iterable must match the array's data type. For example, add multiple elements to an array with a Python list:
๐ŸŒ
freeCodeCamp
freecodecamp.org โ€บ news โ€บ python-list-append-how-to-add-an-element-to-an-array-explained-with-examples
Python List Append โ€“ How to Add an Element to an Array, Explained with Examples
May 8, 2020 - When we call a method, we use a dot after the list to indicate that we want to "modify" or "affect" that particular list. As you can see, the append() method only takes one argument, the element that you want to append. This element can be of any data type: ... Basically, any value that you can create in Python can be appended to a list.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ python_arrays.asp
Python Arrays
Note: Python does not have built-in support for Arrays, but Python Lists can be used instead. Note: This page shows you how to use LISTS as ARRAYS, however, to work with arrays in Python you will have to import a library, like the NumPy library.
๐ŸŒ
IONOS
ionos.com โ€บ digital guide โ€บ websites โ€บ web development โ€บ python append
How to use Python append to extend lists - IONOS
October 30, 2023 - For instance, if you create an array [1, 2, 3, 4, 5], it will have a length of 5. Although Python allows the usage of append() to add an extra element to an array, this operation necessitates the entire array to be deleted and rebuilt in the background, incorporating the additional element. This should be considered when aiming to utilize system resources efficiently. As with lists and arrays, deque data structures in Python behave in a similar manner.
๐ŸŒ
Programiz
programiz.com โ€บ python-programming โ€บ methods โ€บ list โ€บ append
Python List append()
The append() method adds an item to the end of the list. In this tutorial, we will learn about the Python append() method in detail with the help of examples.
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ gloss_python_array_add.asp
Python Add Array Item
Remove List Duplicates Reverse ... Q&A Python Bootcamp Python Certificate Python Training ... You can use the append() method to add an element to an array......
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ python-program-to-append-an-element-into-an-array
Python Program to Append an Element Into an Array
In the below article, we see multiple ways to append an element into an array in Python. As we are using List as an array, we can use the list.append() method to append elements to the array.
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Add an Item to a List in Python: append, extend, insert | note.nkmk.me
April 17, 2025 - In Python, you can add a single item (element) to a list using append() and insert().