That is the wrong mental model for using NumPy efficiently. NumPy arrays are stored in contiguous blocks of memory. To append rows or columns to an existing array, the entire array needs to be copied to a new block of memory, creating gaps for the new elements to be stored. This is very inefficient if done repeatedly.

Instead of appending rows, allocate a suitably sized array, and then assign to it row-by-row:

>>> import numpy as np

>>> a = np.zeros(shape=(3, 2))
>>> a
array([[ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.]])

>>> a[0] = [1, 2]
>>> a[1] = [3, 4]
>>> a[2] = [5, 6]

>>> a
array([[ 1.,  2.],
       [ 3.,  4.],
       [ 5.,  6.]])
Answer from Stephen Simmons on Stack Overflow
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ user โ€บ basics.creation.html
Array creation โ€” NumPy v2.4 Manual
In this example, you did not create a new array. You created a variable, b that viewed the first 2 elements of a. When you added 1 to b you would get the same result by adding 1 to a[:2]. If you want to create a new array, use the numpy.copy array creation routine as such:
๐ŸŒ
W3Schools
w3schools.com โ€บ python โ€บ numpy โ€บ numpy_creating_arrays.asp
NumPy Creating Arrays
Like in above code it shows that arr is numpy.ndarray type. To create an ndarray, we can pass a list, tuple or any array-like object into the array() method, and it will be converted into an ndarray:
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ reference โ€บ routines.array-creation.html
Array creation routines โ€” NumPy v2.5.dev0 Manual
Array creation ยท Note ยท Please refer to Record arrays for record arrays. Note ยท numpy.char is used to create character arrays.
๐ŸŒ
BimStudies
bimstudies.com โ€บ home โ€บ docs โ€บ common python libraries โ€บ programming with python
Array Creation In NumPy | BimStudies.Com
January 13, 2025 - Arrays can be created directly from Python lists or tuples using np.array(). import numpy as np # From a list arr1 = np.array([1, 2, 3, 4]) print(arr1) # From a tuple arr2 = np.array((5, 6, 7, 8)) print(arr2)
Top answer
1 of 16
611

That is the wrong mental model for using NumPy efficiently. NumPy arrays are stored in contiguous blocks of memory. To append rows or columns to an existing array, the entire array needs to be copied to a new block of memory, creating gaps for the new elements to be stored. This is very inefficient if done repeatedly.

Instead of appending rows, allocate a suitably sized array, and then assign to it row-by-row:

>>> import numpy as np

>>> a = np.zeros(shape=(3, 2))
>>> a
array([[ 0.,  0.],
       [ 0.,  0.],
       [ 0.,  0.]])

>>> a[0] = [1, 2]
>>> a[1] = [3, 4]
>>> a[2] = [5, 6]

>>> a
array([[ 1.,  2.],
       [ 3.,  4.],
       [ 5.,  6.]])
2 of 16
149

A NumPy array is a very different data structure from a list and is designed to be used in different ways. Your use of hstack is potentially very inefficient... every time you call it, all the data in the existing array is copied into a new one. (The append function will have the same issue.) If you want to build up your matrix one column at a time, you might be best off to keep it in a list until it is finished, and only then convert it into an array.

e.g.


mylist = []
for item in data:
    mylist.append(item)
mat = numpy.array(mylist)

item can be a list, an array or any iterable, as long as each item has the same number of elements.
In this particular case (data is some iterable holding the matrix columns) you can simply use


mat = numpy.array(data)

(Also note that using list as a variable name is probably not good practice since it masks the built-in type by that name, which can lead to bugs.)

EDIT:

If for some reason you really do want to create an empty array, you can just use numpy.array([]), but this is rarely useful!

๐ŸŒ
Hyperskill
hyperskill.org โ€บ university โ€บ numpy โ€บ numpy-creating-arrays
NumPy Creating Arrays
September 4, 2024 - In this example, list1 is converted into a NumPy array called array1. The data type of the array is automatically determined based on the input elements. If all elements are integers, the array will have an integer data type.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ numpy-array-creation
Numpy - Array Creation - GeeksforGeeks
July 23, 2025 - The most straightforward way to create a NumPy array is by converting a regular Python list into an array using the np.array() function.
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ numpy โ€บ different-ways-to-create-numpy-arrays-in-python
Different Ways to Create Numpy Arrays in Python - GeeksforGeeks
January 31, 2026 - This article covers the most commonly used techniques for creating NumPy arrays, along with when and why to use each method.
๐ŸŒ
SciPy
docs.scipy.org โ€บ doc โ€บ numpy-1.10.1 โ€บ user โ€บ basics.creation.html
Array creation โ€” NumPy v1.10 Manual
October 18, 2015 - This is presumably the most common case of large array creation. The details, of course, depend greatly on the format of data on disk and so this section can only give general pointers on how to handle various formats. Various fields have standard formats for array data. The following lists the ones with known python libraries to read them and return numpy ...
๐ŸŒ
NumPy
numpy.org โ€บ devdocs โ€บ reference โ€บ generated โ€บ numpy.empty.html
numpy.empty โ€” NumPy v2.5.dev0 Manual
Unlike other array creation functions (e.g. zeros, ones, full), empty does not initialize the values of the array, and may therefore be marginally faster. However, the values stored in the newly allocated array are arbitrary. For reproducible behavior, be sure to set each element of the array before reading. ... Try it in your browser! >>> import numpy as np >>> np.empty([2, 2]) array([[ -9.74499359e+001, 6.69583040e-309], [ 2.13182611e-314, 3.06959433e-309]]) #uninitialized
๐ŸŒ
CodeSpeedy
codespeedy.com โ€บ home โ€บ array creation in numpy
Array Creation in NumPy - CodeSpeedy
July 11, 2020 - An empty array can also be created but it is important to note that while creating such an array, all the values are by default initialized with some dummy values. For instance consider the following piece of code: import numpy as np data = np.empty((2)) #creates 1-D array of 5 elements all initialized to garbage values print(data)
๐ŸŒ
Imperial College London
python.pages.doc.ic.ac.uk โ€บ lessons โ€บ numpy โ€บ 02-ndarray โ€บ 02-creation.html
Introduction to NumPy and Matplotlib > Initialising NumPy arrays | Python Programming | Department of Computing | Imperial College London
As mentioned, you can create a np.array instance by passing any sequence type (list or tuples) to constructor of np.array(). In NumPy, a dimension is called an axis (plural: axes).
๐ŸŒ
Wells Fargo
wellsfargojobs.com โ€บ en โ€บ jobs โ€บ r-538068 โ€บ senior-quantitative-analytics-specialist
Senior Quantitative Analytics Specialist - Wells Fargo
1 month ago - We are aware of an increase in fraudulent activity involving individuals posing as Wells Fargo representatives and offering false job interviews or employment opportunities. These scammers may use real employee names and create documents that appear authentic.
๐ŸŒ
DataCamp
datacamp.com โ€บ doc โ€บ numpy โ€บ array
NumPy array()
This example creates a one-dimensional array with specified data type int32, converting all floating-point numbers to integers. import numpy as np arr = np.array([1, 2, 3], ndmin=2)
๐ŸŒ
NumPy
numpy.org โ€บ doc โ€บ stable โ€บ user โ€บ absolute_beginners.html
NumPy: the absolute basics for beginners โ€” NumPy v2.4 Manual
Read more about creating arrays, filled with 0โ€™s, 1โ€™s, other values or uninitialized, at array creation routines.
๐ŸŒ
Medium
medium.com โ€บ @mitchparker99 โ€บ numpy-array-creation-2de97913ff12
Numpy: Array Creation. There are six primary methods forโ€ฆ | by Mitchell Parker | Medium
June 2, 2024 - Numpy: Array Creation There are six primary methods for creating arrays: Conversion from Python Structures: Arrays can be created from Python lists or tuples using numpy.array. Intrinsic NumPy Array โ€ฆ
๐ŸŒ
Medium
medium.com โ€บ @ernestasena โ€บ chapter-2-numpy-essentials-creating-and-manipulating-arrays-4a957b25df27
Chapter 2: NumPy Essentials โ€” Creating and Manipulating Arrays | by Ernest Asena | Medium
August 25, 2023 - These arrays are similar to Python lists but with some key differences, such as homogeneity (all elements must have the same data type) and the ability to perform element-wise operations efficiently. You can create a NumPy array from a Python list using the numpy.array() function:
๐ŸŒ
Quora
quora.com โ€บ How-do-you-create-a-NumPy-array-in-Python
How to create a NumPy array in Python - Quora
The array object in NumPy is called [code ]ndarray[/code]. We can create a NumPy [code ]ndarray[/code] object by using the [code ]array()[/code] function. CODE : [code]import numpy as np arr = np.array([1, 2, 3, 4, 5]) print(arr) print(type...