Skip to main content

Section 9.21 Nested Lists

Let’s say we have information about some students: their name, ID number, and their GPA (grade point average). We can use separate lists for each of these:
names = ["Phuong", "Federico", "Michele", "Steve"]
id_nums = [101345, 43617, 81882, 79315]
gpa = [3.9, 3.92, 3.85, 3.76]
There’s nothing wrong with this, but the data for each student really belongs together. It would be nice if we could put all the data into one list of four elements, where each element has the student name, ID, and GPA grouped together. It turns out that Python lets us do exactly that. We have already seen that lists can contain elements of different data types: [47, "blah", 37.5, True]. Lists are also data types, so we can have a list that contains another list. That lets us write our student information as:
In this list, the element at each index is another list. Each element is called a sublist. A list sequence that contains one or more sublists is called a nested list.
Add the following line to the preceding program: print(len(students)). Before you run the program, what do you think it will print? Will it print 12 (because there are 12 data items all together) or will it print 4 (the number of entries at the “top level” of the list)?
As you saw when you ran the program, len returns the length of only the top-most list. In other words, sublists are considered to be a single item when counting the length of the list.

Subsection 9.21.1 Accessing Sublists and Sublist Elements

Looking at our student information, how would we access Federico’s GPA? We know that it is in the sublist at index 1 in the nested list, and the GPA is at index 2 in that sublist. We can use assignment to temporary variables:
Another easier way to access the element is to use the index operator [ ] twice, giving the main list index first, followed by the sublist index:
This multiple-indexing works for any depth of nesting. Here are our students again, but this time, instead of the GPA, we have another sublist that gives their test scores on the two midterms and the final exam:

Subsection 9.21.2 Check Your Understanding

In these exercises, the nested lists aren’t particularly useful; they are designed to test how well you understand accessing a nested list. We’ll return to more useful nested lists on the next page.

Checkpoint 9.21.1.

    What is printed by the following statements?
    a_list = [ [4, [True, False], 6, 8], [888, 999] ]
    if a_list[0][1][0]:
       print(a_list[1][0])
    else:
       print(a_list[1][1])
    
  • 6
  • 6 is in the wrong list. a_list[1] refers to the second item in a_list, namely [888,999].
  • 8
  • 8 is in the wrong list. a_list[1] refers to the second item in a_list, namely [888,999].
  • 888
  • Yes, a_list[0][1][0] is True and a_list[1] is the second list, whose first item is 888.
  • 999
  • a_list[0][1][0 is True. Take another look at the if statement.

Checkpoint 9.21.2.

    What is printed by the following statements? (Remember: you can use the index operator on strings as well as lists.)
    a_list = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False]
    print(a_list[2][0])
    
  • 56
  • Indexes start with 0, not 1.
  • c
  • Yes, the first character of the string at index 2 is c
  • cat
  • "cat" is the item at index 2, but then we index into it further.
  • Error, you cannot have two index values unless you are using slicing.
  • Using more than one index is fine. You read the indexes from left to right.

Subsection 9.21.3 Cloning Nested Lists

Unlike a simple list, you cannot clone a nested list with [:], as you can see from the following program:
The [:] makes a new copy of the top level list, but the sublists are still references. Instead, you must import the copy module and use its deepcopy function: