2D Lists
Objectives
- Define what a 2D list is in Python and explain its usefulness in representing tabular data with rows and columns.
- Recognize the relationship between 2D lists and tables.
- Demonstrate how to create a 2D list by encapsulating multiple lists within square brackets.
- Understand the structure of a 2D list with examples.
- Explain how to access elements in a 2D list using two indices, one for the row and one for the column.
- Emphasize the need for two indices when working with 2D lists.
- Use nested loops to iterate through the elements of a 2D list.
The examples thus far have only considered lists of one dimension but we can also create lists that look more like tables, with rows and columns, which give us two dimensions. Each element in a 2D list can be accessed using two indices: one for the row and one for the column. More dimensions are possible but not recommended as they can get tricky to handle.
Example 10
Predict what the output of the program will be:
- What will be printed for the original matrix?
- What will be the value of the element at row 2, column 3?
- How will the matrix look after modifying the first element?
Run the program in a Python environment and check your predictions, were they the same?
Creating 2D Lists
You can create a 2D list by enclosing multiple lists within square brackets []
. Each inner list represents a row in the 2D list.
This is a list of lists! Think of it as being organised as a series of rows and columns, like a table.
Accessing Elements
To access an element in a 2D list, use two indices: one for the row and one for the column. Remember that Python uses 0-based indexing.
Modifying 2D Lists
You can change the values of elements in a 2D list by assigning new values using the indices just as you would with 1D lists but now we need two indices.
Looping Through 2D Lists
You can use nested loops to iterate through the elements of a 2D list. The outer loop will work through each row, the inner loop will work through the columns in that row.
Activity
Using the example program above, copy and paste into a Python IDE and make the following modifications:
- Change element at row 3, column 1 to 11
- Add a new row to the matrix
- Remove the second row
Example
We've been asked to create a simple address book of our friends with their name, email address and phone number. Our table of friends might look like this:
Name | Phone | |
---|---|---|
Alice | 09876 444555 | alice@example.com |
Bob | 01287 655444 | bob@hotmail.co.uk |
Charlie | 09998 666555 | charlie_is_great_@gmail.com |
Davina | 06667 543213 | d_khan@myspace.tv |
Edward | 08889 765432 | edward_davies@gmail.com |
We currently have 5 rows and 3 columns, but will want to add more friends later.
First we initialise our empty table (2D list)
Or, we could initialise the table with the data we have already:
Our program will keep running, presenting us with the menu of options, until we enter quit (one of options):
Let's tackle option 2. We could create a separate function for this, which would be preferable if we had to consider validation and other factors such as saving and reading from file etc., but here we're only dealing with three items of data:
Warning
Be careful with the append()
operation, we have to append the list and not each individual item of data
Option 1, finding the friend, is suitable for a function. Here we could write a function that iterates through our table looking for a given name and then return the row when found or an error if not found. This could then be passed to another function to display. This would be best practice so that both functions e.g. find_friend(name)
and display_friend(friend)
would only be handling one task. However, here we'll combine the two for simplicity:
In our main program loop we add the call to that function, once we're got the name to find from our user:
Climate Quest Project
Throughout this topic we'll be working on a large scale project: Climate Quest. In this project a player embarks on a journey to combat the effects of climate change by making decisions that impact the environment. Each choice affects the outcome of the game, emphasizing the importance of individual actions in addressing climate change.
Questions
Programming Tasks
For each of the following tasks write a program using Python.
Write a Python program to manage a simple seating plan. Initialise the seating plan as a 2D list with the first name of the student. Write code (using functions where necessary) to: print the initial seating plan, swap 2 students from one place to another and add a new row of students.
Write a Python program that adds two 2D matrices. Allow the user to input the dimensions and elements of the matrices and display the result.
Write a Python program that calculates and displays the transpose of a given 2D matrix. The transpose of a matrix swaps its rows and columns.
Write a Python program that multiplies two matrices and displays the result. Allow the user to input the dimensions and elements of the matrices.