Open In App

Multi-dimensional Lists in Python

Last Updated : 30 Oct, 2025
Comments
Improve
Suggest changes
25 Likes
Like
Report

In Python, a Multi-dimensional List is a list containing other lists, often used to represent structured data like matrices, tables or 2D arrays. It’s useful for storing and accessing data in rows and columns, commonly applied in data analysis, mathematics and image processing.

For Example:

Python
m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(m)

Output
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Creating a Multidimensional List

A multidimensional list is created by nesting lists within a single list. It allows data to be organized in rows and columns, similar to a matrix or table.

Python
m = [[2,4,6,8,10],
     [3,6,9,12,15],
     [4,8,12,16,20]]
print(m)

Output
[[2, 4, 6, 8, 10], [3, 6, 9, 12, 15], [4, 8, 12, 16, 20]]

Creating a Multidimensional Zero Matrix

A zero matrix is a 2D list with all elements set to zero. It’s commonly used as an initial structure for mathematical operations, data storage or placeholders before inserting actual values.

Python
m, n = 4, 5
mat = []

for i in range(m):
    row = []
    for j in range(n):
        row.append(0)
    mat.append(row)

print(mat)

Output
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]

Explanation:

  • Inner loop for j in range(n) creates one row of zeros.
  • Outer loop for i in range(m) appends each row to the matrix.

Accessing a Multidimensional List

Using Row-by-Row Loop

This approach retrieves each row from the outer list directly. It’s useful when you want to process or print entire rows one at a time without worrying about column indices.

Python
a = [[2, 4, 6, 8, 10], [3, 6, 9, 12, 15], [4, 8, 12, 16, 20]]
for row in a:
    print(row)

Output
[2, 4, 6, 8, 10]
[3, 6, 9, 12, 15]
[4, 8, 12, 16, 20]

Using Index-Based Nested Loops

This method accesses each element using row and column indices. It provides complete control when working with individual elements.

Python
a = [[2, 4, 6, 8], 
     [1, 3, 5, 7], 
     [8, 6, 4, 2], 
     [7, 5, 3, 1]] 
        
for i in range(len(a)):
    for j in range(len(a[i])):
        print(a[i][j], end=" ")
    print()

Output
2 4 6 8 
1 3 5 7 
8 6 4 2 
7 5 3 1 

Explanation:

  • for i in range(len(a)): iterate row indices.
  • for j in range(len(a[i])): iterate column indices for row i.

Methods on Multidimensional Lists

1. Using append()

Adds a new sublist (row) to the end of the outer list. Commonly used when dynamically building matrices or adding data rows.

Python
a = [[2,4,6],[3,6,9]]
a.append([5,10,15])
print(a)

Output
[[2, 4, 6], [3, 6, 9], [5, 10, 15]]

2. Using extend()

Extends a chosen sublist by adding multiple elements from another iterable. Useful for appending more data to an existing row.

Python
a = [[2,4,6],[3,6,9]]
a[0].extend([8,10])
print(a)

Output
[[2, 4, 6, 8, 10], [3, 6, 9]]

3. Using reverse()

Reverses the order of elements within a list. It can be applied either to a specific sublist or to the entire outer list.

Python
a = [[2,4,6],[3,6,9]]

a[1].reverse()   # reverse second row
print(a)
a.reverse()      # reverse row order
print(a)

Output
[[2, 4, 6], [9, 6, 3]]
[[9, 6, 3], [2, 4, 6]]

4. Using Indexing to Read/Write Element

You can directly access or modify elements in a specific position using double indexing syntax a[i][j].

Python
a = [[1,2],[3,4]]

print(a[0][1])   # read
a[1][0] = 9      # write
print(a)

Output
2
[[1, 2], [9, 4]]

5. Using List Comprehension for Processing Rows

List comprehensions provide a compact way to modify or filter elements in rows or columns, ideal for data transformations like scaling or filtering.

Python
a = [[1, 2, 3], [4, 5, 6]]
b = [[ x*2 for x in row ] for row in a] 
print(b)

Output
[[2, 4, 6], [8, 10, 12]]

Article Tags :

Explore