Two-Dimensional List
Pay close attention, folks, because 2D lists are basically tables.Tables are two-dimensional data structures where we can store data both vertically and horizontally.
Usually this means that vertical data is used for fields (one category - name, ID, favorite biscuit, etc.) and horizontal data is used for records (all the data for each category).
Behind the scenes, we see a list inside a list. Forget what you know about reading a table with math or geography: 'across the corridor (x-axis) first and then down the stairs (y-axis)'.
Here, we will do row index first and then the column index.
Remember...
Here's a 1D list. We have the list name as a variable, single equals to set the value, and sqaure brackets to show this is a list. 2D lists are very similar.
my1DList = ["Johnny", 21, "Mac"]
Each new list has its own set of square brackets and is separated by a comma. This layout of code is nice to help us visualise the 2D list as a table, but...
my2DList = [ ["Johnny", 21, "Mac"],
["Sian", 19, "PC"],
["Gethin", 17, "PC"] ]
my2DList = [ ["Johnny", 21, "Mac"], ["Sian", 19, "PC"], ["Gethin", 17, "PC"] ]
Printing From a 2D List
Remember, any comments about the code are written in green like this:
the entire list
We can print an entire 2D list just like we do with a 1D list. However, this will output (print) all of the square brackets, commas, etc.
print(my2DList)
a single row
To print a single row, use a single square bracket [] in the print command. However, you will still get all of those square brackets and commas.
In this example, I'm outputting the first row (index 0) - all of the data about Johnny.
print(my2DList[0])
a single piece of data
Editing a 2D List
We can edit values in a 2D list in the same way as variables and 1D lists. You just need to change the value to the new row and column index numbers.To print a single row, use a single square bracket [] in the print command. However, you will still get all of those square brackets and commas.
In this example, I'm outputting the first row (index 0) - all of the data about Johnny.
The first square bracket references the list, while the second references the item inside that list.
Here are a couple of examples:my2DList = [ ["Johnny", 21, "Mac"],
["Sian", 19, "PC"],
["Gethin", 17, "PC"] ]
print(my2DList[0][0])
# This code outputs 'Johnny'. It's Johnny's name from list 0 (first square bracket), item 0 (second square bracket)
print(my2DList[1][2])
# This code outputs 'PC'. It's Sian's computing preferene from list 1 (first square bracket), item 2 (second square bracket)
In this example, Sian has joined the dark side, so we're updating her computing preference to Linux.
my2DList = [ ["Johnny", 21, "Mac"],
["Sian", 19, "PC"],
["Gethin", 17, "PC"] ]
my2DList[1][2] = "Linux"
# The line above changes list 1, item 2 from PC to Linux
print(my2DList[1])
# I'm using this line to output list 1 to check that the change has happened correctly.
Coding Challenge -
Randomly generate a series of number between 0 and 90.
Allocate each number to a place in a 2D list.
The numbers should be in numerical order, left to right.
Numbers should not be repeated.
The center square should not contain a number. It should contain the word 'BINGO!'.
import random
bingo = []
def ran():
number = random.randint(1,90)
return number
def prettyPrint():
for row in bingo:
print(row)
numbers = []
for i in range(8):
numbers.append(ran())
numbers.sort()
bingo = [ [ numbers[0], numbers[1], numbers[2]],
[ numbers[3], "BINGO", numbers[4] ],
[ numbers [5], numbers[6], numbers[7]]
]
prettyPrint()