To initialize a two-dimensional list in Python, use

t = [ [0]*3 for i in range(3)]

But don't use [[v]*n]*n, it is a trap!

>>> a = [[0]*3]*3
>>> a
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
>>> a[0][0]=1
>>> a
[[1, 0, 0], [1, 0, 0], [1, 0, 0]]
Answer from Jason CHAN on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-using-2d-arrays-lists-the-right-way
Using 2D arrays/lists in Python - GeeksforGeeks
The code below, compares two ways of initializing a 2D list in Python. Using list multiplication ([[0]*cols]*rows) creates multiple references to the same inner list, causing aliasing where changes affect all rows. Using a nested list comprehension creates a separate list for each row, avoiding aliasing and correctly forming a 2D array.
Published: December 20, 2025
๐ŸŒ
Snakify
snakify.org โ€บ two-dimensional lists (arrays)
Two-dimensional lists (arrays) - Learn Python 3 - Snakify
In real-world Often tasks have to store rectangular data table. [say more on this!] Such tables are called matrices or two-dimensional arrays. In Python any table can be represented as a list of lists (a list, where each element is in turn a list).
Discussions

How to initialize a two-dimensional array (list of lists, if not using NumPy) in Python? - Stack Overflow
List multiplication makes a shallow copy. When you assign to an index, it does a proper change, but access does not, so when you do a[x][y] = 2, it's accessing, not assigning, for the xth index - only the yth access is actually changed. This page helped me explain with diagrams that are probably better than what I tried explaining in this comment: geeksforgeeks.org/python-using-2d-arrays... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Python - Conversion of list of arrays to 2D array - Stack Overflow
The comments and answers pointing ... tuples, lists, or np.arrays themselves) must have the same length. If they don't, you'll still get A.shape = (3,) and A will have dtype=object. I've definitely been snagged by this, when elements unexpectedly had different lengths. ... Sign up to request clarification or add additional context in comments. ... If I understood correctly what you're asking, you have a case where numpy did not convert array of arrays into 2d ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
2D array of lists in python - Stack Overflow
I am trying to create a 2d matrix so that each cell contains a list of strings. Matrix dimensions are known before the creation and I need to have access to any element from the beginning (not popu... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Explain 2D Lists
Lists contain things. Lists are things. Therefore, lists can contain lists. That's literally all there is to it. More on reddit.com
๐ŸŒ r/learnpython
6
2
August 3, 2014
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python-using-2d-arrays-lists-the-right-way
Python | Using 2D arrays/lists the right way - GeeksforGeeks
Let's start by looking at common ways of creating a 1d array of size N initialized with 0s. Manually initializing and populating a list without using any advanced features or constructs in Python is known as creating a 1D list using "Naive Methods". ... Here we are multiplying the number of rows by the empty list and hence the entire list is created with every element zero. ... Using 2D arrays/lists the right way involves understanding the structure, accessing elements, and efficiently manipulating data in a two-dimensional grid.
Published: June 20, 2024
Top answer
1 of 4
23

Not sure if I understood the question correctly, but does this work for you?

import numpy as np
A = [[1,2,3],[4,5,6],[7,8,9]]
A = np.array(A)

If A is a list of numpy array, how about this:

Ah = np.vstack(A)
Av = np.hstack(A)
2 of 4
8

If I understood correctly what you're asking, you have a case where numpy did not convert array of arrays into 2d array. This can happen when your arrays are not of the same size. Example:

Automatic conversion to 2d array:

import numpy as np
a = np.array([np.array([1,2,3]),np.array([2,3,4]),np.array([6,7,8])])
print a

Output:

>>>[[1 2 3]
    [2 3 4]
    [6 7 8]]

No automatic conversion (look for the change in the second subarray):

import numpy as np
b = np.array([np.array([1,2,3]),np.array([2,3,4,5]),np.array([6,7,8])])
print b

Output:

>>>[array([1, 2, 3]) array([2, 3, 4, 5]) array([6, 7, 8])]

I found a couple of ways of converting an array of arrays to 2d array. In any case you need to get rid of subarrays which have different size. So you will need a mask to select only "good" subarrays. Then you can use this mask with list comprehensions to recreate array, like this:

import numpy as np

a = np.array([np.array([1,2,3]),np.array([2,3,4,5]),np.array([6,7,8])])
mask = np.array([True, False, True])

c = np.array([element for (i,element) in enumerate(a) if mask[i]])

print a
print c

Output:

>>>>[array([1, 2, 3]) array([2, 3, 4, 5]) array([6, 7, 8])]
>>>>[[1 2 3]
     [6 7 8]]

Or you can delete "bad" subarrays and use vstack(), like this:

import numpy as np

a = np.array([np.array([1,2,3]),np.array([2,3,4,5]),np.array([6,7,8])])
mask = np.array([True, False, True])

d = np.delete(a,np.where(mask==False))
e = np.vstack(d)

print a
print e

Output:

>>>>[array([1, 2, 3]) array([2, 3, 4, 5]) array([6, 7, 8])]
>>>>[[1 2 3]
     [6 7 8]]

I believe second method would be faster for large arrays, but I haven't tested the timing.

๐ŸŒ
Guru99
guru99.com โ€บ home โ€บ python โ€บ python 2d arrays: two-dimensional list examples
Python 2D Arrays: Two-Dimensional List Examples
July 10, 2026 - Use a list comprehension such as [[0]*cols for _ in range(rows)]. Avoid [[0]*cols]*rows, because it repeats the same inner list reference, so editing one row accidentally changes every row. ๐Ÿ“ How do you find the number of rows and columns in a 2D array? Use len(array) for the number of rows and len(array[0]) for the columns in the first row. With NumPy, the shape attribute returns both values as a (rows, columns) tuple. โš–๏ธ What is the difference between a Python list and a NumPy array?
Find elsewhere
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ 2d array in python
2D Array in Python | Python Two-Dimensional Array - Scaler Topics
May 25, 2026 - Square brackets are the notation used to define the nested lists in this form. This syntax is as follows: Where array_name is the arrayโ€™s name, r1c1, r1c1 etc., are elements of the array. Here r1c1 means that it is the element of the first column of the first row. A 2D array is an array of arrays. Choose from our industry-leading programs designed for career success ... We can directly access values or elements of a 2D array in Python...
๐ŸŒ
iO Flood
ioflood.com โ€บ blog โ€บ python-2d-array
Python 2D Array with Lists | Guide (With Examples)
February 10, 2024 - In this example, weโ€™ve created a 2D array (or a matrix) with three rows and three columns. Each inner list [1, 2, 3], [4, 5, 6], and [7, 8, 9] represents a row in the 2D array. When we print the array, we get the output as a nested list, which is the Pythonic way of representing a 2D array.
๐ŸŒ
Processing
py.processing.org โ€บ tutorials โ€บ 2dlists
Two-Dimensional Lists \ Tutorials
Python Mode for Processing extends the Processing Development Environment with the Python programming language.
๐ŸŒ
Dot Net Perls
dotnetperls.com โ€บ 2d-python
Python - 2D List Examples - Dot Net Perls
To construct a 2D list, we can use append() or an initializer. We create an empty list and add empty lists to it with append(). We can construct any rectangular (or jagged) list this way. In this example we build a 2 by 2 list. Step 1 We first create an empty list with the empty square brackets.
๐ŸŒ
Finxter
blog.finxter.com โ€บ home โ€บ learn python blog โ€บ python list to 2d array โ€“ the ultimate conversion guide
Python List to 2D Array - The Ultimate Conversion Guide - Be on the Right Side of Change
October 27, 2023 - A popular library for working with arrays in Python is NumPy. NumPy provides a more efficient and versatile way to work with arrays, including 2D arrays. Converting a list of lists to a NumPy 2D array can be done using the following code:
๐ŸŒ
Note.nkmk.me
note.nkmk.me โ€บ home โ€บ python
Convert 1D Array to 2D Array in Python (numpy.ndarray, list) | note.nkmk.me
May 15, 2023 - l = [0, 1, 2, 3, 4, 5] print(np.array(l).reshape(-1, 3).tolist()) # [[0, 1, 2], [3, 4, 5]] print(np.array(l).reshape(3, -1).tolist()) # [[0, 1], [2, 3], [4, 5]] ... See the following article on how to convert numpy.ndarray and list to each other. ... If NumPy is not available, you can still achieve the transformation using list comprehensions, range(), and slices. ... def convert_1d_to_2d(l, cols): return [l[i:i + cols] for i in range(0, len(l), cols)] l = [0, 1, 2, 3, 4, 5] print(convert_1d_to_2d(l, 2)) # [[0, 1], [2, 3], [4, 5]] print(convert_1d_to_2d(l, 3)) # [[0, 1, 2], [3, 4, 5]] print(convert_1d_to_2d(l, 4)) # [[0, 1, 2, 3], [4, 5]]
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 29149286 โ€บ list-of-lists-to-2d-array-in-python
List of Lists to 2D Array in Python - Stack Overflow
If so, is there any other type of data that I can use? Is setting it to Arrayobject ensures that I can combine str/int inside of it? ... You need to use Array.CreateInstance to create 2D arrays, and they have to be of a single type.
Top answer
1 of 16
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 16
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.

๐ŸŒ
Beauty and Joy of Computing
bjc.edc.org โ€บ March2019 โ€บ bjc-r โ€บ cur โ€บ programming โ€บ old-labs โ€บ python โ€บ 2D_lists.html
2D Lists in Python
All of the previously mentioned list functions still operate on 2D lists: >>> cart[0][2] = "cabbage" >>> cart [ ["kale", "spinach", "cabbage"], ["olives", "tomatoes", "avocado"]] Now that you have some experience with lists in Python, try writing a function that takes a list of lists (2D) and returns all the items of each list concatenated together into one new list as shown below:
๐ŸŒ
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.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to convert a list to a 2d array?
r/learnpython on Reddit: How to convert a list to a 2D array?
May 5, 2020 -

I have a list:

data=[1,2,3,4,5,6,7,...]

and I want to transform it into a 2-dimensional array, with 5 columns and 10 rows

How can I do it?

๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ several lists into one 2d matrix
r/learnpython on Reddit: several lists into one 2d matrix
August 15, 2023 -

Hello, I have a simple question : For example, I have 3 lists a,b,c , and I want to join them into one big 2d array called d, how do I do it:

a= [1,2,3]

b= [4,5,6]

c= [7,8,9]

result wanted :

d= [

[1,2,3],

[4,5,6],

[7,8,9]

]

thank you !!!