Multi-Dimensional Lists

Setting up a multi-dimensional list


Lists can be given another dimension to hold data that is related to each other.

A scenario: Three students have taken two Chemistry tests and their teacher has recorded the results in a 2 dimensional array (note that Python does not use arrays but uses lists instead):

lists19

To add this to Python we would write:

lists20


Printing a multi-dimensional list


To print each record in the list (I have altered the generic ‘i’ count variable to be more descriptive):

lists21

This would output:

lists22


Printing a specific record


To print a record from its index number (look at the table above and remember that Python starts counting at 0: so Edward is record 0, Bella 1 and Jacob 2):

lists23

This would output:

lists27


Printing a specific data value


To print a specific data value then you need to define the record number and then the data index. Use the table above to help you visualise this:

lists24

Edward is the zero record and the first value is his 34 score:  lists28


Searching for a specific value


To search through a multi-dimensional list then you need to search through each record and then each data element for a specific value:

lists25

This would output:

lists26


< Back to Section Eight