fbpx

Multi-Dimensional Lists

Aa multidimensional list is a data structure that can store values in multiple dimensions, typically in the form of a grid or a matrix. It is essentially a list of lists, where each element of the outer list contains an inner list.

In Python, we can implement this using nested lists. Here’s an example of a multidimensional array using nested lists:

				
					# Creating a 2-dimnesional list
grid = [[1, 2, 3],
        [4, 5, 6],
        [7, 8, 9]]

# Accessing elements of the list
print(grid[0][0])  # Output: 1
print(grid[1][2])  # Output: 6
print(grid[2][1])  # Output: 8

# Modifying elements of the list
grid[1][1] = 10
print(grid)  # Output: [[1, 2, 3], [4, 10, 6], [7, 8, 9]]

				
			

The above code shows us how the use of two-dimensional lists make it more practical for us to store information that might be best presented in a grid or table format. When indexing a 2D list, the first set of hard brackets accepts the row index, and the second set of hard brackets accepts the column index, if we visualize it as a table.

It is possible to use similar syntax to create lists of more than two dimensions, but can become a lot to handle as it is more difficult to visualize beyond two or three dimensions.

Replit Practice

Loading...