🌐
W3Schools
w3schools.com › python › numpy › numpy_creating_arrays.asp
NumPy Creating Arrays
An array that has 1-D arrays as its elements is called a 2-D array. These are often used to represent matrix or 2nd order tensors. NumPy has a whole sub module dedicated towards matrix operations called numpy.mat
Spaces
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
Practice
Practice coding problems on W3Schools. Write code, submit, and get instant feedback.
W3Schools app iOS & Android NEW
The W3Schools app turns coding fundamentals into a game. Bite-sized lessons, streaks and XP. Free on iOS and Android.
Python For Loops
A for loop is used for iterating over a sequence (that is either a list, a tuple, a dictionary, a set, or a string).
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_reshape.asp
NumPy Array Reshaping
Try converting 1D array with 8 elements to a 2D array with 3 elements in each dimension (will raise an error): import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7, 8]) newarr = arr.reshape(3, 3) print(newarr) Try it Yourself » · Check if the returned array is a copy or a view: import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7, 8]) print(arr.reshape(2, 4).base) Try it Yourself » ·
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_iterating.asp
NumPy Array Iterating
import numpy as np arr = np.array([1, 2, 3]) for idx, x in np.ndenumerate(arr): print(idx, x) Try it Yourself » · Enumerate on following 2D array's elements: import numpy as np arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]]) for idx, x in np.ndenumerate(arr): print(idx, x) Try it Yourself » ·
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_shape.asp
NumPy Array Shape
The shape of an array is the number of elements in each dimension. NumPy arrays have an attribute called shape that returns a tuple with each index having the number of corresponding elements.
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_indexing.asp
NumPy Array Indexing
You can access an array element by referring to its index number. The indexes in NumPy arrays start with 0, meaning that the first element has index 0, and the second has index 1 etc.
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_slicing.asp
NumPy Array Slicing
Slice elements from index 4 to the end of the array: import numpy as np arr = np.array([1, 2, 3, 4, 5, 6, 7]) print(arr[4:]) Try it Yourself »
🌐
AskPython
askpython.com › python › array › multidimensional-arrays
Multidimensional Arrays in Python: A Complete Guide - AskPython
February 27, 2023 - In this article, the creation and implementation of multidimensional arrays (2D, 3D as well as 4D arrays) have been covered along with examples in Python Programming language. To understand and implement multi-dimensional arrays in Python, the NumPy package is used.
🌐
freeCodeCamp
freecodecamp.org › news › multi-dimensional-arrays-in-python
Multi-Dimensional Arrays in Python – Matrices Explained with Examples
December 11, 2025 - In this example, we first import the NumPy library using the import statement. Then, we create a 2-dimensional array using the np.array() function and pass in a list of lists as an argument. Each inner list represents a row of the array, and the outer list contains all the rows.
Top answer
1 of 7
1263

You're technically trying to index an uninitialized array. You have to first initialize the outer list with lists before adding items; Python calls this "list comprehension".

# Creates a list containing 5 lists, each of 8 items, all set to 0
w, h = 8, 5
Matrix = [[0 for x in range(w)] for y in range(h)] 

#You can now add items to the list:

Matrix[0][0] = 1
Matrix[6][0] = 3 # error! range... 
Matrix[0][6] = 3 # valid

Note that the matrix is "y" address major, in other words, the "y index" comes before the "x index".

print Matrix[0][0] # prints 1
x, y = 0, 6 
print Matrix[x][y] # prints 3; be careful with indexing! 

Although you can name them as you wish, I look at it this way to avoid some confusion that could arise with the indexing, if you use "x" for both the inner and outer lists, and want a non-square Matrix.

2 of 7
487

If you really want a matrix, you might be better off using numpy. Matrix operations in numpy most often use an array type with two dimensions. There are many ways to create a new array; one of the most useful is the zeros function, which takes a shape parameter and returns an array of the given shape, with the values initialized to zero:

>>> import numpy
>>> numpy.zeros((5, 5))
array([[ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.],
       [ 0.,  0.,  0.,  0.,  0.]])

Here are some other ways to create 2-d arrays and matrices (with output removed for compactness):

numpy.arange(25).reshape((5, 5))         # create a 1-d range and reshape
numpy.array(range(25)).reshape((5, 5))   # pass a Python range and reshape
numpy.array([5] * 25).reshape((5, 5))    # pass a Python list and reshape
numpy.empty((5, 5))                      # allocate, but don't initialize
numpy.ones((5, 5))                       # initialize with ones

numpy provides a matrix type as well, but it is no longer recommended for any use, and may be removed from numpy in the future.

Find elsewhere
🌐
W3Schools
w3schools.com › python › python_arrays.asp
Python Arrays
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.
🌐
PyTutorial
pytutorial.com › python-2d-arrays-guide-lists-numpy-examples
PyTutorial | Python 2D Arrays Guide: Lists, NumPy, Examples
March 25, 2026 - Python does not have a built-in 2D array type. But we can create them easily. The most common ways are using nested lists or the NumPy library.
🌐
Python Guides
pythonguides.com › python-numpy-2d-array
Create A 2D NumPy Array In Python (5 Simple Methods)
May 9, 2025 - Cells, rows, columns and slices of a 2D sales array. import numpy as np matrix = [[1, 2], [3, 4]] matrix.append([5, 6]) # add a row to a nested list for row in matrix: row.append(0) # add a column print(matrix) arr = np.array([[1, 2], [3, 4]]) arr = np.vstack([arr, [5, 6]]) # add a row (returns a new array) arr = np.hstack([arr, np.zeros((3, 1), dtype=int)]) # add a column arr[0, 0] = 99 # update one cell print(arr) print("transposed:\n", arr.T)
🌐
W3Schools
w3schools.com › python › numpy › numpy_array_split.asp
NumPy Splitting Array
If the array has less elements than required, it will adjust from the end accordingly. ... import numpy as np arr = np.array([1, 2, 3, 4, 5, 6]) newarr = np.array_split(arr, 4) print(newarr) Try it Yourself »
🌐
Drbeane
drbeane.github.io › python_dsci › pages › array_2d.html
2-Dimensional Arrays — Python for Data Science
The arrays we have worked with ... dimensional arrays. In this lesson, we will work exclusively with 2D arrays, which consist of several values arranged into ordered rows and columns....
🌐
Sentry
sentry.io › sentry answers › python › define a two-dimensional array in python
Python Define a Two-Dimensional Array or Matrix | Sentry
September 23, 2026 - The following code will create a 3-by-3 2D array with all values set to zero: import numpy matrix = numpy.zeros((3,3)) # array([[0., 0., 0.], # [0., 0., 0.], # [0., 0., 0.]])
🌐
w3resource
w3resource.com › python-exercises › numpy › python-numpy-exercise-37.php
Python NumPy: Create a 2-dimensional array of size 2 x 3 - w3resource
August 29, 2025 - Write a NumPy program to create a 2-dimensional array of size 2 x 3 (composed of 4-byte integer elements), also print the shape, type and data type of the array. ... # Importing the NumPy library with an alias 'np' import numpy as np # Creating ...
🌐
NumPy
numpy.org › doc › stable › reference › arrays.ndarray.html
The N-dimensional array (ndarray) — NumPy v2.5 Manual
An ndarray is a (usually fixed-size) multidimensional container of items of the same type and size. The number of dimensions and items in an array is defined by its shape, which is a tuple of N non-negative integers that specify the sizes of each dimension. The type of items in the array is ...
🌐
NumPy
numpy.org › devdocs › user › absolute_beginners.html
NumPy: the absolute basics for beginners — NumPy v2.6.dev0 Manual
Basic operations are simple with NumPy. If you want to find the sum of the elements in an array, you’d use sum(). This works for 1D arrays, 2D arrays, and arrays in higher dimensions.
🌐
W3Schools
w3schools.com › python › numpy › default.asp
NumPy Tutorial
NumPy is a Python library. NumPy is used for working with arrays.
🌐
Programiz
programiz.com › python-programming › numpy › ndarray-creation
NumPy N-D Array Creation (With Examples)
May 22, 2023 - 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)