Loading...
Hello, my name is Mrs. Jones, and I'm really pleased that you're here to learn with me today.
Today's lesson is called 2D arrays and lists, and we are going to look at how to store and use multiple lists.
So let's get started.
Welcome to today's lesson called 2D arrays and lists from the unit Programming: strings and lists.
By the end of this lesson, you'll be able to define and use two-dimensional lists.
There are three keywords to today's lesson.
List.
List is a dynamic data structure that can contain items of different data types.
Two-dimensional.
Two-dimensional describes a list or array that has both columns and rows of values.
And array.
Array is a static data structure that contains items of the same data type.
There are two sections to today's lesson.
The first is Define A 2D array and a list, and the second is Use a 2D list in a programme.
So let's start with Define A 2D array and a list.
Sofia is asking, "I know I can store the scores from my game in a list, but how can I store the players' names as well? I guess I could use two lists." Izzy replies by saying, "Or you could use a two-dimensional list." Sofia says, "What's that?" Two-dimensional arrays and lists allow you to store multiple arrays or lists in one variable.
These can be useful to store all relevant information together.
You can see there the arrow across the top saying one dimensional, and down the left, two-dimensional.
Note, it is possible to have more than two dimensions, but these can be difficult to handle and are outside the scope of this lesson.
So 2D arrays or lists can be visualised as a table of rows and columns.
This 2D list called scores shows the five scores of five named players.
You have the columns and the rows.
Each column and row can be referred to by its index number.
Note, they start from zero on both rows and columns.
If this two-dimensional list is called scores, then to return the list of names, you would use scores and then inside the square brackets, 0, because that is the row.
Let's have a quick check.
What expression would return the integer list of players' scores from the 2D list? Is it a, scores[0], b, scores[1], or c, scores[2]? Pause the video to consider your answer, and then we'll check it.
Let's check your answer.
The answer is b, scores[1] Well done if you got that correct.
Sofia says, "Remind me, what's the difference between a list and an array?" And Izzy says, "Lists are dynamic and can contain different data types.
Arrays are static and contain the same data type." In Python, they are both represented in the same way.
Sofia says, "So how do I represent a 2D list in Python?" It's really good question, so let's have a look.
Essentially, a 2D list or array is a list of lists and you can see here our scores = on line 1 and we open a square bracket.
And then on line 2 and 3, you have the lists with their own square brackets, separated by a comma.
And then on line 4, we close the square bracket that was opened on line 1.
So note the positions of the square brackets.
There is a set around each row, plus a set around both rows.
There is also a comma between each row.
Let's have a quick check.
Can you spot the syntax error in the representation of a 2D list? You can see the programme there for shopping, and we have two lists within the list, 2D list.
So is it a, it is missing a square bracket, b, there are too many square brackets, or c, it's missing a comma between the two lists.
Pause the video to consider your answer, Let's check your answer.
The answer was C.
Let's check your answer.
The answer was c, it's missing a comma between the two lists.
Well done if you got that correct.
To access and print out a row in a 2D list, you specify the row index in square brackets.
Here we've got the scores = [.
We then have a list of names, comma, followed by a list of integers of scores, and then on line 4, we close that square bracket.
But on line 5, we've got print, and inside the brackets, we have scores, and inside the square brackets, 0, and the text output then is the list and the index 0, where we have Izzy, Alex, Jun, Aisha, and Sam.
You can see there that that is the important element.
The 0 returns that row index.
So let's change it.
Then now we have the row index of 1, which will return the integer values, the scores, because we're now returning that list instead of the one on row index 0.
To access and print out a single item in a 2D list, you specify the row index in square brackets, followed by the column index in square brackets.
So in this one, we now have print(scores).
So we have the first square bracket, which is 0, which is the row index, followed by the square brackets with 2 inside, which is the column index.
So we know we're looking at the names because that's on row index 0, and we are going to move along to position index 2, which is Jun.
And there we go.
We can see that's there.
This time, we've got row index 1 and column index 0 within the print scores on line 5, and that's gonna return 45.
Let's have a quick check.
Which Python instruction returns the price of bread from the 2D list named shopping? Is it a, shopping[1][1], b, shopping[1][0], or c, shopping[0][1]? Pause the video to consider your answer, and then we'll go through it.
Let's check your answer.
The answer was a, shopping[1], which was the row index, and [1], which was the column index.
Well done if you got that correct.
Let's have a look at an activity, and I want you to have a go at writing the Python code to define the following 2D lists.
We've got grades and inside there, we want to have the Computing, Physics and Art and A, B, and D.
The next 2D list is gonna be called board, and there we've got the 0s and the Xs, and the third one is planets.
And this time, we've got three rows in this one with the planets.
We got some numbers on both of those, which will be explained after.
So pause the video and have a go at creating those three 2D lists, and then we'll go through the solutions.
Let's go through the solutions.
So the first one, which was looking at grades, we've got grades = and we open the square brackets.
On line 2, we have the list with the square brackets, and each of those, Computing, Physics, Art, with speech marks around them and a comma in between.
At the end of that first list, outside the square brackets, we have a comma.
On line 3, we have the next list set up with square brackets and inside the speech marks, A, B, D, separated by a comma.
On line 4, we have the square bracket closed.
Well done if you got that one correct.
Let's have a look at the next solution, which was for the board.
board =, and again, we've got the open and close of the brackets on lines 1 and 5, and inside, we've now got three lines, three lists.
So each list is set up, and outside the list on lines 2 and 3, there is a comma to separate.
Well done if you've got that one correct.
And let's look at the third one for planets.
And again, we've got planets = and we open and close those square brackets on line 1 and 5, and difference on this one is the first list is string, so they all have quote marks around it.
And on lines 3 and 4, those are all integers and floats, so you have got no quote marks around those.
Well done if you've got those correct.
Let's have a look at the next part of this activity.
Using the existing 2D list planets, which holds data on five planets and their rotation and orbit times, so notice on the table on that slide, you can see the first row is the name of the planet.
The second row, so the second list, is rotation time, and the third is orbit time.
And we want to output the following.
We want to print out the following: a, the name of the fourth planet, b, Jupiter's rotation time, and c, Earth's orbit time.
Pause the video, and have a go at using that list, that 2D list, planets, to complete this activity, and then we'll go through the solution.
Let's have a look at the solution.
So we've got the 2D list set up on lines 1 to 5, and then on line 6, we have print(planets[0][3]), which returns Mars.
On lin 7, we have print(planets) and this time, in the square brackets, we have 1 and then 4, which returns Jupiter's rotation time, 0.
42 and then the final line, in the square bracket, this time, we have 2 and 2, which is the Earth's orbit time, 365.
25.
Well done if you got that correct.
Let's move on to the second part of today's lesson.
Use a 2D list in a programme.
To replace an item in a one-dimensional list, you need to specify the location and provide the value that you wish to replace it with, in this case, "Cow".
You can see here that Assign Cow at location 0.
On line 2, it says animals[0] = "Cow".
It is similar process with a 2D list, except you must specify both the indexes to identify the value in the list you want to update.
Assign Plaice at location 0,2.
Let's have a quick check.
Which Python instruction will change "Camel" to "Leopard" in this 2D list? Is it a, animals[0][2] = "Leopard"? Is it b, animals[2][0] = "Leopard"? Is it c, animals[2, 0] = "Leopard", or is it d, animals[0][2] = Leopard? Pause the video to consider your answer, and then we'll go through it.
Let's check your answer.
The answer was b, animals, and then inside the first is the row index, which is 2, and then in the second square brackets is the column index 0 and replace that with "Leopard." defined after the equals in speech marks.
Well done if you've got that correct.
To add a new row to a 2D list, you can use the append() method.
For example, to add a new row of scores to this table representing the 2D list scores, you can use scores.
append([10, 25, 40, 33, 49]) which are the new scores, all separated by comma.
You can see here that on line 6 is where that's been added, and the new row has been appended to the end of the existing row.
Let's have a quick check.
If the 2D list board is initialised as shown in A, what instruction has been run to change it to B? We can see that we have an extra row on B.
Is it a, board.
append("0","X","0")? Is it b, board.
append["0","X","0"]? Or is it c, board.
append(["0","X","0"])? Pause the video to consider your answer, and then we'll check it.
Let's check your answer.
The answer was c, board.
append(["0","X","0"]) You need to remember both brackets.
Well done if you got that correct.
Looping through a list of numeric values to calculate a total or an average is a common technique with one-dimensional lists.
You can see an example here.
We have initialising a total value to 0.
We loop through the list.
rainfall[i] is assigned to each value in the list.
In turn, this is added to the total.
Print the total outside the loop, and calculate the average by dividing by the number of values.
You can see the text output is total rainfall was 45 millimetres, and average rainfall was 7.
5 millimetres.
This same technique can be used with 2D lists with just one adjustment, adding the index of the row of interest.
So here you can see that line 5 and line 8 have been adjusted to show that change.
Let's have a look at an activity.
Using the existing 2D list with data on five students, loop through the scores in the second row, and print out the total score for all five students, and then print out the average score for the five students.
Pause the video, and have a go at creating that 2D list adjustment for total score and average score, and then we'll go through the solution.
Let's have a look at the solution.
We have the scores, and we have that 2D list set up at the start.
On line 5, we have total = 0 for i in range(5) total = total + scores[0][i] Output, (f"Total score: {total}") average = total / 5 print output, (f"Average score: {average}"), and we have the total score equals 301, and the average score at 60.
2 well done if got that correct.
Let's have a look at another activity, and I want you to modify your code to find the name of the student who scored the highest.
You'll need to add two new variables.
Set high_score as 0 and high_name as "" as a blank string.
Inside the for loop check if the current score value is larger than the high_score.
If it is, then set high_score to this value.
Also use the index i to access the name stored in the row above and assign it to high_name.
Print high_score and high_name at the end.
Pause the video and have a go at modifying your code, and then we'll go through the solution.
Let's have a look at a solution, and what we've got here is we have total =0, and there we have high_score = 0 high_name = "" for i in range(5) total = total + scores[1][i] if scores[1][1] > high_score: high_score = scores[1][1] high_name equals scores[0][i] Then we've got the previous output for total and average, but on line 16, we now have print (The highest score was {high_score} scored by {high_name}.") So now the output will show the original, but also the highest score was 84 scored by Aisha.
Well done if you've got that correct.
In summary, two-dimensional lists and arrays in Python are essentially a list of lists.
They are used when you want to store related items in rows and columns within one variable.
Two-dimensional lists are created in Python using the square bracket syntax.
New rows can be appended using the append function.
Rows can be accessed by using a single index number.
Individual items within the 2D list can be accessed using two index numbers for the row and column of that item.
Well done for completing this lesson on 2D arrays and lists.