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.4 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), and via the methods and attributes of the ndarray.
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.
Videos
Create a Matrix (2D array) in NumPy Python | Module NumPy ...
05:14
Python Two Dimensional Numpy Arrays - YouTube
08:16
2D Numpy Arrays for Data Science | Complete Python Tutorial - YouTube
07:43
NumPy multidimensional arrays are easy! π§ - YouTube
17:50
How to create a 2-dimensional array in NumPy Python | Create ...
02:20
Create a Matrix (2D array) in NumPy Python | Module NumPy Tutorial ...
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()
NumPy
numpy.org βΊ devdocs βΊ user βΊ absolute_beginners.html
NumPy: the absolute basics for beginners β NumPy v2.5.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.
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: ... A dimension in arrays is one level of array depth (nested arrays). nested array: are arrays that have arrays as their elements. 0-D arrays, or Scalars, are the elements in an array. Each value in an array is a 0-D array.
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.4 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 ...
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.
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.
W3Schools
w3schools.com βΊ python βΊ numpy βΊ numpy_array_iterating.asp
NumPy Array Iterating
import numpy as np arr = np.array([1, 2, 3]) for x in np.nditer(arr, flags=['buffered'], op_dtypes=['S']): print(x) Try it Yourself Β» Β· We can use filtering and followed by iteration. Iterate through every scalar element of the 2D array skipping 1 element:
NumPy
numpy.org βΊ devdocs βΊ user βΊ basics.creation.html
Array creation β NumPy v2.5.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 ...
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)
Top answer 1 of 2
13
You can use fill method to init the array.
x = np.empty(shape=(800,800))
x.fill(1)
2 of 2
1
Found out the answer myself: This code does what I want, and shows that I can put a python array ("a") and have it turn into a numpy array. For my code that draws it to a window, it drew it upside down, which is why I added the last line of code.
# generate grid
a = [ ]
allZeroes = []
allOnes = []
for i in range(0,800):
allZeroes.append(0)
allOnes.append(1)
# append 400 rows of 800 zeroes per row.
for i in range(0, 400):
a.append(allZeroes)
# append 400 rows of 800 ones per row.
for i in range(0,400):
a.append(allOnes)
#So this is a 2D 800 x 800 array of zeros on the top half, ones on the bottom half.
array = numpy.array(a)
# Need to flip the array so my other code that draws
# this array will draw it right-side up
array = numpy.flipud(array)
Python Examples
pythonexamples.org βΊ python-numpy-create-2d-array
Create 2D Array in NumPy
The function returns a numpy array with specified shape, and all elements in the array initialised to zeros. import numpy as np # create a 2D array with shape (3, 4) shape = (3, 4) arr = np.zeros(shape) print(arr)
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