Login
📚 Introduction to Python Programming
Chapters ▾
⇩ Download ▾

9.4 Nested lists

Learning objectives

By the end of this section you should be able to

  • 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.