This works:
a = [[1, 2, 3], [4, 5, 6]]
nd_a = np.array(a)
So this should work too:
nd_a = np.array([[x for x in y] for y in a])
Answer from Marijn van Vliet on Stack OverflowNumPy
numpy.org โบ doc โบ stable โบ reference โบ arrays.ndarray.html
The N-dimensional array (ndarray) โ NumPy v2.5 Manual
The type of items in the array is specified by a separate data-type object (dtype), one of which is associated with each ndarray. As with other container objects in Python, the contents of an ndarray can be accessed and modified by indexing or slicing the array (using, for example, N integers), ...
Top answer 1 of 3
21
This works:
a = [[1, 2, 3], [4, 5, 6]]
nd_a = np.array(a)
So this should work too:
nd_a = np.array([[x for x in y] for y in a])
2 of 3
11
To create a new array, it seems numpy.zeros is the way to go
import numpy as np
a = np.zeros(shape=(x, y))
You can also set a datatype to allocate it sensibly
>>> np.zeros(shape=(5,2), dtype=np.uint8)
array([[0, 0],
[0, 0],
[0, 0],
[0, 0],
[0, 0]], dtype=uint8)
>>> np.zeros(shape=(5,2), dtype="datetime64[ns]")
array([['1970-01-01T00:00:00.000000000', '1970-01-01T00:00:00.000000000'],
['1970-01-01T00:00:00.000000000', '1970-01-01T00:00:00.000000000'],
['1970-01-01T00:00:00.000000000', '1970-01-01T00:00:00.000000000'],
['1970-01-01T00:00:00.000000000', '1970-01-01T00:00:00.000000000'],
['1970-01-01T00:00:00.000000000', '1970-01-01T00:00:00.000000000']],
dtype='datetime64[ns]')
See also
- How do I create an empty array/matrix in NumPy?
- np.full(size, 0) vs. np.zeros(size) vs. np.empty()
07:43
NumPy multidimensional arrays are easy! ๐ง - YouTube
16:05
NumPy Techniques and Practical Examples: Populating & Changing ...
08:16
2D Numpy Arrays for Data Science | Complete Python Tutorial - YouTube
How to Create 1D, 2D, and 3D Arrays in NumPy
05:14
Python Two Dimensional Numpy Arrays - YouTube
DataCamp
campus.datacamp.com โบ courses โบ intro-to-python-for-data-science โบ chapter-4-numpy
2D NumPy Arrays | Python
The arrays np_height and np_weight are one-dimensional arrays, but it's perfectly possible to create 2 dimensional, three dimensional, heck even seven dimensional arrays! Let's stick to 2 in this video though. You can create a 2D numpy array from a regular Python list of lists.
NumPy
numpy.org โบ devdocs โบ user โบ absolute_beginners.html
NumPy: the absolute basics for beginners โ NumPy v2.6.dev0 Manual
Using np.newaxis will increase the dimensions of your array by one dimension when used once. This means that a 1D array will become a 2D array, a 2D array will become a 3D array, and so on.
Python Guides
pythonguides.com โบ python-numpy-2d-array
Create A 2D NumPy Array In Python (5 Simple Methods)
May 16, 2025 - In this article, Iโll show you five easy methods to create 2D NumPy arrays (also known as matrices) based on my decade of experience working with Python.
OpenGenus
iq.opengenus.org โบ 2d-array-in-numpy
2D Arrays in NumPy (Python)
October 28, 2022 - For working with numpy we need to first import it into python code base. ... To get a specific element from an array use arr[r,c] here r specifies row number and c column number.
Drbeane
drbeane.github.io โบ python_dsci โบ pages โบ array_2d.html
2-Dimensional Arrays โ Python for Data Science
The arrays we have worked with up to this point have all been one-dimensional arrays which consist of a sequence of numbers in a linear order. Numpy provides us with tools for creating and working with higher dimensional arrays. In this lesson, we will work exclusively with 2D arrays, which consist of several values arranged into ordered rows and columns.
NumPy
numpy.org โบ doc โบ stable โบ user โบ basics.creation.html
Array creation โ NumPy v2.5 Manual
NumPy arrays can be defined using Python sequences such as lists and tuples. Lists and tuples are defined using [...] and (...), respectively. Lists and tuples can define ndarray creation: a list of numbers will create a 1D array, a list of lists will create a 2D array, further nested lists ...
Programiz
programiz.com โบ python-programming โบ numpy โบ ndarray-creation
NumPy N-D Array Creation (With Examples)
We saw how to create N-d NumPy arrays from Python lists. Now we'll see how we can create them from scratch. To create multidimensional arrays from scratch we use functions such as ... The np.zeros() function allows us to create N-D arrays filled with all zeros. For example, ... # create 2D array with 2 rows and 3 columns filled with zeros array1 = np.zeros((2, 3)) print("2-D Array: ") print(array1)
GeeksforGeeks
geeksforgeeks.org โบ python-using-2d-arrays-lists-the-right-way
Python | Using 2D arrays/lists the right way - GeeksforGeeks
Given a 3D list, the task is to convert it into a 2D list. These type of problems are encountered while working on projects or w ... Python's NumPy package makes slicing multi-dimensional arrays a valuable tool for data manipulation and analysis.
Published: June 20, 2024
Fabienmaussion
fabienmaussion.info โบ intro_to_programming โบ week_09 โบ 01-multidim-numpy.html
Multi-dimensional numpy arrays โ Introduction to Programming
For the final two weeks of the semester, you will learn to manipulate multi-dimensional arrays (ndarrays) which are very common in meteorology, climatology, and geosciences in general. import numpy as np import matplotlib.pyplot as plt
NumPy
numpy.org โบ devdocs โบ user โบ basics.creation.html
Array creation โ NumPy v2.6.dev0 Manual
NumPy arrays can be defined using Python sequences such as lists and tuples. Lists and tuples are defined using [...] and (...), respectively. Lists and tuples can define ndarray creation: a list of numbers will create a 1D array, a list of lists will create a 2D array, further nested lists ...
Scaler
scaler.com โบ home โบ topics โบ matplotlib โบ how to visualize a 2d array?
How to Visualize a 2D Array? | Scaler Topics
June 5, 2024 - Matplotlib and Numpy provide the modules and functions to visualize a 2D array in Python. To visualize an array or list in matplotlib, we have to generate the data, which the NumPy library can do, and then plot the data using matplotlib. There are many functions by which we can add data to ...