# Two-Dimensional Lists in Python

October 29, 2023

In Python, a two-dimensional list, often referred to as a 2-D list or a list of lists, is a data structure that allows you to create a grid-like structure of elements. Each element in a 2-D list can be accessed using two indices – one for the row and another for the column. Two-dimensional lists are commonly used to represent tables, matrices, grids, and other structured data.

## Creating a 2-D List

To create a 2-D list in Python, you can use nested square brackets. The outer bracket defines the list, and each element of this list is itself a list representing a row. Each inner list can have a different length, making it a versatile data structure.

Here’s an example of creating a simple 2-D list:

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

In this example, `matrix`

is a 2-D list containing three rows, each of which contains three elements.

## Iterating through a 2-D List

To traverse the elements of a 2-D list, you can use nested loops. Typically, two nested loops are used: one to iterate over rows, and another to iterate over elements within each row. You can achieve this using either the `range()`

function or the `in`

operator.

### Using the `range()`

Function:

Here’s how you can iterate through a 2-D list using `range()`

:

# Iterating through the 2-D list using range() for i in range(len(matrix)): # Iterate over rows for j in range(len(matrix[i]): # Iterate over elements in each row print(matrix[i][j], end=' ') print() # Move to the next row

This code first loops through the rows and then, for each row, loops through the elements in that row. It prints each element on the same line and moves to the next row after completing each row.

### Using the `in`

Operator:

You can also use the `in`

operator to simplify the iteration process:

# Iterating through the 2-D list using the "in" operator for row in matrix: # Iterate over rows for element in row: # Iterate over elements in each row print(element, end=' ') print() # Move to the next row

In this code, we directly iterate through rows and elements in a more Pythonic way, making the code cleaner and more readable.

## Example

Let’s put it all together with an example:

# Creating a 2-D list matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] # Iterating through the 2-D list using the "in" operator for row in matrix: for element in row: print(element, end=' ') print()

The output of this code will be:

1 2 3 4 5 6 7 8 9

This demonstrates the creation and iteration of a 2-D list in Python, which is a valuable data structure for various applications, such as representing grids, tables, or matrices in your programs.

Here’s another example to show you can print a 12×12 multiplication table in Python using nested loops.

for i in range(1, 13): for j in range(1, 13): # Calculate the product of i and j product = i * j # Use f-strings to format the output print(f"{i} x {j} = {product}\t", end='\t') print() # Move to the next row after each inner loop

This code uses two nested loops to iterate through the numbers from 1 to 12 for both rows and columns. It calculates the product of the current row and column indices and prints each multiplication along with proper formatting. The `end='\t'`

argument in the `print()`

function ensures that the results are separated by tabs, creating a nicely formatted table.

When you run this code, it will print a 12×12 multiplication table in the console.