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 ...
🌐
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:
🌐
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.
🌐
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 - While Python-append is most used for managing lists, it can be applied to other common data structures in Python. However, there are a few important differences that you need to be aware of. Python Arrays aren’t part of the standard Python library and must be imported from an external Python ...
🌐
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.
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.append.html
numpy.append — NumPy v2.4 Manual
>>> np.append([[1, 2, 3], [4, 5, 6]], [7, 8, 9], axis=0) Traceback (most recent call last): ... ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)
🌐
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.
🌐
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......