Like other programming languages, Python has different data structures and lists are one of them. Lists can store multiple values in a linear fashion. For some cases like matrices or tables, a list needs to be two-dimensional to store the tabular data properly.
In this post, we will discuss different ways of initializing a two-dimensional (2D) list in Python.
What is a 2D list in Python?
A 2D list in Python is a list that contains other lists as its elements. For example:
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Here matrix
is a 2D list that contains 3 lists, each representing a row in the matrix.
So in a 2D list:
- Each inner list represents a row
- And each element in the inner list represents a column value
Why Use 2D Lists?
2D lists are useful for storing tabular data in Python. For example:
- Representing a matrix or grid
- Storing data from a database table
- Representing images in pixel format
They provide a clean way to organize data into rows and columns for processing. We can easily access any row or column just like with normal lists.
Ways to Initialize a 2D List
There are several ways to initialize an empty or populated 2D list in Python:
- Using Nested List Comprehensions
- Using
numpy
arrays - Using the
append()
method - Assigning lists to rows
Let‘s look at examples of each method.
1. Nested List Comprehension
We can create a 2D list with a specified size using nested for
loops inside list comprehensions:
# Create a 3x5 2D list full of 0‘s
matrix = [[0 for col in range(5)] for row in range(3)]
print(matrix)
# [[0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0]]
Here:
- Outer loop runs 3 times to create 3 rows
- Inner loop runs 5 times to create 5 columns in each row
We can also initialize it with some other default value:
# Initialize with 1‘s instead
matrix = [[1 for col in range(5)] for row in range(3)]
print(matrix)
# [[1, 1, 1, 1, 1],
# [1, 1, 1, 1, 1],
# [1, 1, 1, 1, 1]]
The list comprehension technique provides a concise way to create a 2D list with our desired number of rows and columns.
2. Using numpy Arrays
The NumPy library contains a powerful N-dimensional array object that can be used to represent a 2D grid or matrix.
To convert a numpy array into a nested list, we can use the .tolist()
method:
import numpy as np
# Create a 3x5 numpy array full of 1‘s
np_arr = np.ones((3, 5))
# Convert it into a list
matrix = np_arr.tolist()
print(matrix)
# [[1.0, 1.0, 1.0, 1.0, 1.0],
# [1.0, 1.0, 1.0, 1.0, 1.0],
# [1.0, 1.0, 1.0, 1.0, 1.0]]
The key thing is that numpy
handles much of the complication behind the scenes. So we get a simple way to initialize a 2D list with any values we want.
3. Using append()
We can build up a 2D list row by row using the .append()
method:
# Initialize an empty list
matrix = []
for row in range(3):
# Append an empty sublist inside the list
matrix.append([])
print(matrix)
# [[,], [,], [,]]
for row in range(3):
for col in range(5):
matrix[row].append(col)
print(matrix)
# [[0, 1, 2, 3, 4],
# [0, 1, 2, 3, 4],
# [0, 1, 2, 3, 4]]
So:
- First add empty rows
- Then append elements to populate each row
This gives precise control while building the 2D list.
4. Assign Lists to Indices
We can also directly assign list objects as rows:
# Create list of lists
row1 = [1, 2, 3]
row2 = [4, 5, 6]
row3 = [7, 8, 9]
# Assign to matrix indices
matrix = []
matrix.append(row1)
matrix.append(row2)
matrix.append(row3)
print(matrix)
# [[1, 2, 3],
# [4, 5, 6],
# [7, 8, 9]]
This is useful when we already have data organized by rows that we want to assign directly to initialize the 2D list.
Populating Elements
Along with creation, we can populate the nested lists with values in a few ways:
# Nested loop
for row in range(3):
for col in range(5):
matrix[row][col] = row*col
print(matrix)
# List comprehension
[[col*row for col in range(5)] for row in range(3)]
# numpy populate
np_arr = np.arange(15).reshape(3, 5)
matrix = np_arr.tolist()
So in summary, Python gives great flexibility in terms of creating and populating a 2D list with values.
Accessing Elements
To access a 2D matrix, we can use nested index notation:
matrix = [[1,2,3], [4,5,6], [7,8,9]]
# Get first row
print(matrix[0])
# Get first element of second row
print(matrix[1][0])
# Get last element of last row
print(matrix[-1][-1])
The outer index selects the list representing the desired row.
And the inner index fetches the element from that row.
This provides an intuitive way to index matrix data in row/column format.
Applications of 2D Lists
Some common use cases of 2D lists in Python:
- Store tables of data from files or databases for processing
- Represent images and videos in pixel format
- Serve as matrices for linear algebra calculations
- Represent tilegrids or grids in games
- Tabular data for machine learning algorithms
They provide a useful data structure when working with multi-dimensional data across Python libraries like NumPy, Pandas, OpenCV, PyGame etc.
So in summary:
- 2D lists organize data into rows and columns
- Useful for matrices, grids, tables and other tabular datasets
- Flexible initialization and manipulation in Python
By leveraging tools like list comprehensions and NumPy, we can efficiently create 2D lists to handle complex multi-dimensional data.