Skip to ContentGo to accessibility pageKeyboard shortcuts menu
OpenStax Logo

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.

Checkpoint

List-of-lists

Concepts in Practice

Lists

For each of the questions below, consider the following matrix:


mat A= 7 4 5 3 9 6 1 2 8 mat A= 7 4 5 3 9 6 1 2 8
1.
What would be the correct way to represent matA in Python?
  1. [[7, 4, 5], [3, 9, 6], [1, 2, 8]]
  2. [7, 4, 5
    3, 9, 6
    1, 2, 8]
  3. [[7, 3, 1], [4, 9, 2], [1, 2, 8]
2.
What would be the correct index for the number 6 in the above list?
  1. [5]
  2. [2][1]
  3. [1][2]
3.
What would be the result of the following code:
  print(matA[0])
  1. Error
  2. 7
  3. [7, 4, 5]

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.

Example 9.4

Iterating a list-of-lists

The code below demonstrates how to iterate a list-of-lists.

The outer loop on line 9 goes element by element for the larger list. Each element in the larger list is a list. The inner loop on line 10 iterates through each element in each nested list.

1
"""Iterating a list-of-lists."""
2
3
# Create a list of numbers
4
list1 = [[1, 2, 3],
5
        [1, 4, 9],
6
        [1, 8, 27]]
7
8
# Iterating the list-of-lists
9
for row in list1:
10
  for num in row:
11
    print(num, end=" ")
12
  print()

The above code's output is:

    1 2 3
    1 4 9
    1 8 27
    

Concepts in Practice

Iterating a list-of-lists

For each question below, consider the following list:

my_list = [[7, 4, 5, 12],
          [24, 3, 9, 16],
          [12, 8, 91, -5]]
4.
Which code prints each number in my_list starting from 7, then 4, and so on ending with -5?
  1. for row in my_list:
      for elem in row:
        print(elem)
    
  2. for elem in my_list:
        print(elem)
    
5.
The range() function can also be used to iterate a list-of-lists. Which code prints each number in my_list starting from 7, then 4, and so on, ending with -5, using counting for loops?
  1. for column_index in range(0, len(my_list[0])):
      for row_index in range (0, len(my_list)):
        print(my_list[row_index][column_index])
    
  2. for row_index in range(0, len(my_list)):
      for column_index in range (0, len(my_list)):
        print(my_list[row_index][column_index])
      print()
    
  3. for row_index in range(0, len(my_list)):
      for column_index in range (0, len(my_list[0])):
        print(my_list[row_index][column_index])
    

Try It

Matrix multiplication

Write a program that calculates the matrix multiplication product of the matrices matW and matZ below and prints the result. The expected result is shown.

mat W= 13 4 5 2 9 7 7 3 19 mat Z= 2 1 5 3 7 9 1 13 19 mat W= 13 4 5 2 9 7 7 3 19 mat Z= 2 1 5 3 7 9 1 13 19
result= 33 106 196 30 30 62 4 275 423 result= 33 106 196 30 30 62 4 275 423

In the result matrix, each element is calculated according to the position of the element. The result at position [i][j] is calculated using row i from the first matrix, W, and column j from the second matrix, Z.

Ex:

result[1][2] = (row 1 in W) times (column 2 in Z)

result[1][2]= 2 -9 7 * 5 9 19 result[1][2]= 2 -9 7 * 5 9 19


result[1][2]=2*5+(-9)*9+7*19=10-81+133=62 result[1][2]=2*5+(-9)*9+7*19=10-81+133=62
Citation/Attribution

This book may not be used in the training of large language models or otherwise be ingested into large language models or generative AI offerings without OpenStax's permission.

Want to cite, share, or modify this book? This book uses the Creative Commons Attribution License and you must attribute OpenStax.

Attribution information
  • If you are redistributing all or part of this book in a print format, then you must include on every physical page the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
  • If you are redistributing all or part of this book in a digital format, then you must include on every digital page view the following attribution:
    Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction
Citation information

© Mar 15, 2024 OpenStax. Textbook content produced by OpenStax is licensed under a Creative Commons Attribution License . The OpenStax name, OpenStax logo, OpenStax book covers, OpenStax CNX name, and OpenStax CNX logo are not subject to the Creative Commons license and may not be reproduced without the prior and express written consent of Rice University.

This book utilizes the OpenStax Python Code Runner. The code runner is developed by Wiley and is All Rights Reserved.