Introduction:

In Python, we often need to work with more complex structures than simple lists. One such structure is a 2D list, also called a matrix. A 2D list is essentially a list of lists, allowing you to organise data in rows and columns, much like a table or spreadsheet.

In other programming languages, a similar concept is called a 2D array, where data is stored in a grid of rows and columns. In Python, although we don’t have traditional arrays, 2D lists provide a flexible and powerful way to represent data in a tabular form.

  • 1D List: A single list that contains a sequence of values.
  • 2D List (Matrix): A list where each element is itself a list, allowing us to represent data in rows and columns.

Real-World Example:

Think about a simple spreadsheet where each row represents a different student, and each column represents the scores they achieved in different subjects. This kind of structured data can be easily stored and manipulated using a 2D list in Python.

Creating 2D Lists:

You can create a 2D list by nesting lists inside a main list. Each inner list represents a row of data, and you can access elements using two indices—one for the row and one for the column.

Here’s a basic example:

# A 2D list representing a table of numbers
matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ]

In this example, the outer list contains three inner lists, each representing a row of numbers.

 

Accessing Elements in a 2D List:

You can access elements in a 2D list using two indices:

  1. The first index selects the row.
  2. The second index selects the column within that row.

For example, to access the number 6 in the matrix:

 print(matrix[1][2])

Here, matrix[1] accesses the second row ([4, 5, 6]), and matrix[1][2] accesses the third element (6) in that row. The output will be:

6

 

Adding (Appending) to a 2D List:

You can add new rows to a 2D list using the append() method. This adds an entire new row (which is a list) at the end of the matrix.

For example:

# Adding a new row to the matrix
matrix.append([10, 11, 12]) print(matrix)

Now, the matrix will have four rows:

[[1, 2, 3],
 [4, 5, 6],
 [7, 8, 9],
 [10, 11, 12]]

Removing Items from a 2D List:

You can remove an entire row from the matrix using the pop() method. If you want to remove the second row (index 1), you would do the following:

# Removing the second row from the matrix
matrix.pop(1)
print(matrix)

After removing the second row, the matrix will look like this:

[[1, 2, 3],
[7, 8, 9],
[10, 11, 12]]

To remove specific elements from a row, you can use del. For example, to remove the second element (2) from the first row:

# Removing the second element from the first row
del matrix[0][1] print(matrix)

The updated matrix will be:

[[1, 3],
[7, 8, 9],
[10, 11, 12]]

Modifying Elements in a 2D List:

You can modify specific elements in a 2D list by accessing them with their row and column indices. For example, to change the value 9 in the matrix to 99:

# Changing the value of 9 to 99
matrix[1][2] = 99
print(matrix)

The modified matrix will be:

[[1, 3],
[7, 8, 99],
[10, 11, 12]]

By understanding how to create, access, add to, remove from, and modify 2D lists in Python, you can handle more complex data structures, such as tables, grids, and matrices, with ease.

Predict:

Let’s look at this code. Can you predict what will happen when we access a specific element in the 2D list?

matrix = [ [10, 20, 30], [40, 50, 60], [70, 80, 90] ]
print(matrix[1][2]) 

Think about how indexing works. The first index (matrix[1]) will access the second row, and the second index (matrix[1][2]) will access the third element in that row. What do you think the output will be?

Run:

Now, let’s try running a simple 2D list in Python. This example creates a 2D list of student scores in three subjects and prints out the scores for the second student.

When you run the code, see how the list stores the data in rows. The second student’s scores (in the second list) are printed.

Investigate:

Let’s investigate further! You can access individual elements in a 2D list using two indices: the first to select the row and the second to select the column. Here’s a slightly modified example where we’ll print a specific score from a student.

Instructions: Run the code and see what happens. Try changing the indices to access other students’ scores. What do you observe when you change the values of the row or column indices?

Modify:

It’s your turn to modify the code. Below is a 2D list representing a grid of numbers, but there is a mistake in the code. The program is supposed to print the value 7 from the third row, but it isn’t working correctly. Can you fix the code so that it prints the correct value?

Hint: Check the indices carefully! Remember that Python lists are zero-indexed, so the first row is at index 0, the second row at index 1, and so on.

Scroll to Top