To create a Two-Dimensional List in Python, you can use list comprehensions or nested loops to initialize the elements.
In this example,
matrix
to represent the 2D list.matrix = [[i * 3 + j for j in range(3)] for i in range(3)]
print(matrix)
[[0, 1, 2], [3, 4, 5], [6, 7, 8]]
In this example,
matrix
to represent the 2D list.matrix = [['Element {}{}'.format(i, j) for j in range(3)] for i in range(3)]
print(matrix)
[['Element 00', 'Element 01', 'Element 02'], ['Element 10', 'Element 11', 'Element 12'], ['Element 20', 'Element 21', 'Element 22']]
In this tutorial, we learned How to create a Two Dimensional List in Python language with well detailed examples.