Two Dimensional Lists

CT04b

  • Define the term ‘array’

  • Define the term ‘list’

  • Give characteristics of one-dimensional and two-dimensional data structures

  • Use indexing to access any item in a two-dimensional structure

  • Use ‘for’ to iterate over every item in a two-dimensional structure

Lists

CT04b

Previously, you learned about lists and arrays.

An array is a data structure that allows you to store multiple values under a single identifier.

Python uses lists to implement arrays.

Lists are accessed using an index, starting with 0.

For example, this list contains a set of integers.

list[5] contains 93.

Lists can be iterated (looped through, from front to back).

list

23 42 12 16 75 93 13 66 27 92

Tables

CT04b

In the last lesson you learned that presenting information in a table format makes output more user friendly.

Programmers also use tables to store information.

In Python, you can create a structure that resembles a table by nesting a list inside a list.

This means creating a list in which each item is also a list.

This is referred to as a two-dimensional list.

In this example, each row in the table is represented by one list and the whole table is represented by an outer list.

table

16 54 12
65 32 98
45 84 78
48 59 34

Indexing a table

CT04b

You can access the individual elements of a two-dimensional list using the [ ].

In order to access a single element, you need to choose which row to access, and then which column.

For example, the cell in the table holding 98 is accessed by:

table

16 54 12
65 32 98
45 84 78
48 59 34

table [1] [2]

Row
number

Column
number

Activity 1

CT04b

Complete the table to show that you understand the use of indexing in a 2-dimensional data structure (list).

theHouse = [["oven", "hob", "sink", "toaster", "refrigerator"],
            ["bed", "mattress", "table", "mirror", "cupboard"],
            ["sofa", "television", "chair", "carpet", "foot stool"],
            ["shower", "bath tub", "toilet", "sink", "towel rail"]]
           
theNumbers = [[10, 20, 30, 40, 50, 60, 70],
              [11, 22, 33, 44, 55, 66, 77],
              [111, 222, 333, 444, 555, 666, 777]]
theNumbers[2][3]
444
theHouse[0][1]
bed
theHouse[0][1]
hob
theHouse[0][0]
oven

Statement

Output

theNumbers[0][3]
40
theNumbers[1][6]
77
theNumbers[2][1]
222
len(theNumbers)
3
len(theHouse)
4
len(theNumbers[1])
7
theHouse[0]
['oven','hob','sink','toaster','refigerator']
theNumbers[1]
[11, 22, 33, 44, 55, 66, 77]

Defining a table

CT04b

Creating a two-dimensional list in Python is straightforward. 

This is the code to create the table shown below.

table = [[16, 54, 12], [65, 32, 98], [45, 84, 78], [48, 59, 34]]

table

16 54 12
65 32 98
45 84 78
48 59 34

Row 0

Row 1

Row 2

Row 3

This set is the first row in the table, accessed with table[0]

Row 1, column 2 so accessed using table[1] [2]

Activity 2

CT04b

What do you think the code will do?

What output do you think it will produce? 

# --- Global Variables ---
shelves = [["soda", "sweets", "crisps"],
           ["corn", "peas", "beans"],
           ["cereal", "oatmeal", "granola"]]

# --- Main Program ---
print(shelves[1][1])
print(shelves[2][0])
print(f"My favourites are {shelves[0][2]} and {shelves[2][1]}." )

Activity 2

CT04b

# --- Global Variables ---
shelves = [["soda", "sweets", "crisps"],
           ["corn", "peas", "beans"],
           ["cereal", "oatmeal", "granola"]]

# --- Main Program ---
print(shelves[1][1])
print(shelves[2][0])
print(f"My favourites are {shelves[0][2]} and {shelves[2][1]}.")

Load the code ‘Activity-2-PRIMM-Student.py’ into Thonny and run it.

Did it do what you thought it would do? 

Activity 2

CT04b

# --- Global Variables ---
shelves = [["soda", "sweets", "crisps"],
           ["corn", "peas", "beans"],
           ["cereal", "oatmeal", "granola"]]

# --- Main Program ---
print(shelves[1][1])
print(shelves[2][0])
print(f"My favourites are {shelves[0][2]} and {shelves[2][1]}.")

What would happen if the indices on line 9 were reversed?

What would be printed if this line was added? 

What would be printed if this line was added?

print(shelves[1])
print(shelves[-2][-1])

Iterating a 2D list

CT04b

You iterate a table in the same way that you iterate a list.

You use a for loop to look at each sub-list (row) in turn. 

table

16 54 12
65 32 98
45 84 78
48 59 34
for row in table:
    print(row)
for row in table:
  for item in row:
    print(item)
[16, 54, 12]
[65, 32, 98]
[45, 84, 78]
[48, 59, 34]
16
54
12
65
32
98
45
84
78
48
59
34

Records

CT04b

Tables are commonly used to store records.

# table headers: Name, Assignments, Lates, Attendance
classData = [["Alice", 10, 1, 0.89],
             ["Bob", 12, 0. 0.93],
             ["Charley", 9, 2, 0.87],
             ["Daisy", 12, 1, 0.99],
             ["Ellis", 11, 0, 0.93]]

A record is a row in a table that can contain different data types.

This approach is commonly used to create a database where each record relates to one person or individual item.

For example a two-dimensional array can be used to store information about students in a class.

Review

CT04b

Define the term ‘array’.

  • A data structure where all elements are the same data type.

Define the term ‘list’.

  • A data structure where elements can be different types.

Give characteristics of one-dimensional and two-dimensional data structures.

  • One-dimensional structures are lists and arrays.
  • Two-dimensional structures are tables, created by nesting 1-D structures together.

Use indexing to access any item in a two-dimensional structure.
Use [ ] [ ] to access specific rows and columns.
Use ‘for’ to iterate over every item in a two-dimensional structure.
Nested for loops allow you to access every single element in a table.

ct04b 2D Lists

By David James

ct04b 2D Lists

  • 391