Demonstrate the use of a list-of-lists to structure data.
Demonstrate individual element addressing using multi-dimensional indexing.
Use nested loops to iterate a list-of-lists.
List-of-lists
Lists can be made of any type of element. A list element can also be a list. Ex: [2, [3, 5], 17] is a valid list with the list [3, 5] being the element at index 1.
When a list is an element inside a larger list, it is called a nested list. Nested lists are useful for expressing multidimensional data. When each of the elements of a larger list is a smaller list, the larger list is called a list-of-lists.
Ex: A table can be stored as a two-dimensional list-of-lists, where each row of data is a list in the list-of-lists.
Using nested loops to iterate nested lists
A nested loop structure can be used to iterate a list-of-lists. For a two-dimensional list-of-lists, an outer for loop can be used for rows, and an inner for loop can be used for columns.
Adapted from Introduction to Python Programming by OpenStax (openstax.org), licensed under CC BY-NC-SA 4.0. Changes were made. License: CC-BY-NC-SA-4.0.
These eBooks are a prerelease and are not yet certified conformant with WCAG 2.1 AA or any other accessibility standard. Every page is built against an automated accessibility gate, and our target for the published editions is WCAG 2.2 Level AA. If something is unusable, please tell us.