top of page
top

## Creating a List with Multiple Dimensions

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):

To create this in Python:

## Printing a 2D List

To print the whole list, use a for loop to cycle through each record.

â€‹

I have altered the normal i variable to be 'record', so it is more descriptive:

Use the index number to print a specific record.

Look at the table above and remember that Python starts counting at 0 so Edward is record 0, Bella 1 and Jacob 2:

To print a specific data value, you need to define the record number and then the data index.

â€‹

When using 2D lists, the first value is the row, and the second value is the column.

Use the table at the very top to help you visualise this:

Use the introduction at the top to help you create a 2D list with three friends in the first column, their age in the second column and their favourite colour in the third column.

â€‹

Print the whole list.

â€‹

Then print just the second person's information.

Example solution:

## Searching Through a 2D List

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

Use the 2D list that you created in the first practice task.

â€‹

Ask the user to enter a name.

â€‹

Search through the list and print the record of that person's name.

Example solution:

bottom of page