Every problem in the chapter beside the book’s answer, a chapter at a time as you scroll.

Chapter 9: Lists

29 problems · 0 figures

Concepts in Practice 9.1.1 §9.1 source ↗

Which operation can be used to add an element to the end of a list?
  1. a.
    add()
  2. b.
    append()
  3. c.
    pop()

Answer

B. append()
The append() operation adds an element to the end of a list.

Concepts in Practice 9.1.2 §9.1 source ↗

What is the correct syntax to remove the element 23 from a list called number_list?
  1. a.
    remove()
  2. b.
    number_list.remove()
  3. c.
    number_list.remove(23)

Answer

C. number_list.remove(23)
The element to be removed is passed as a parameter to the remove operation. The remove is performed on the specified list.

Concepts in Practice 9.1.3 §9.1 source ↗

Which operation can be used to remove an element from the end of a list?
  1. a.
    only pop()
  2. b.
    only remove()
  3. c.
    either pop() or remove()

Answer

C. either pop() or remove()
Both pop() and remove() can be used to remove the last element of a list. pop() automatically removes the last element, whereas remove() requires the last element be specified.

Concepts in Practice 9.1.4 §9.1 source ↗

For the following questions, consider the list:

my_list = [2, 3, 5, 7, 9]
How many times will the following for loop execute?
for element in my_list:
  1. a.
    5
  2. b.
    4

Answer

A. 5
The length of the list is 5, so the iterative for loop executes five times.

Concepts in Practice 9.1.5 §9.1 source ↗

For the following questions, consider the list:

my_list = [2, 3, 5, 7, 9]
What is the final value of i for the following counting for loop?
for i in range(0, len(my_list)):
  1. a.
    9
  2. b.
    4
  3. c.
    5

Answer

B. 4
len(my_list) is 5. A counting for loop ends at 1 less than the second value in the range function, so the final value of i is 4 .

Concepts in Practice 9.1.6 §9.1 source ↗

For the following questions, consider the list:

my_list = [2, 3, 5, 7, 9]
What is the output of the code below?
for i in range(0, len(my_list), 2):
  print(my_list[i], end=' ')
  1. a.
    2 5 9
  2. b.
    2 3 5 7 9
  3. c.
    2
    5
    9

Answer

A. 2 5 9
The for loop steps by two each time, so index 0, 2, and 4 are printed. Due to the "end=" in the print statement, all elements are printed on one line.

Concepts in Practice 9.2.1 §9.2 source ↗

What would be the last element of the following list if it is sorted in descending order?
[12, 3, 19, 25, 16, -3, 5]
  1. a.
    25
  2. b.
    -3
  3. c.
    5

Answer

B. -3
Sorting in descending order means to arrange the elements from largest to smallest. -3 is the smallest element of the list and would be the last element when the list is sorted in descending order.

Concepts in Practice 9.2.2 §9.2 source ↗

Arrange the following list in ascending order.
["cat", "bat", "dog", "coyote", "wolf"]
  1. a.
    ["bat", "cat", "coyote", "dog", "wolf"]
  2. b.
    ["wolf", "coyote", "dog", "cat", "bat"]

Answer

A. ["bat", "cat", "coyote", "dog", "wolf"]
For strings, ascending order refers to dictionary order.

Concepts in Practice 9.2.3 §9.2 source ↗

How are the words "flask" and "flash" related in Python?
  1. a.
    "flask" < "flash"
  2. b.
    "flask" == "flash"
  3. c.
    "flask" > "flash"

Answer

C. "flask" > "flash"
In alphabetical order, "h" comes before "k". The first four characters are the same, so "flash" comes before "flask" .

Concepts in Practice 9.2.4 §9.2 source ↗

Use the following list for the questions below.

board_games = ["go", "chess", "scrabble", "checkers"]
What is the correct way to sort the list board_games in ascending order?
  1. a.
    sort(board_games)
  2. b.
    board_games.sort()
  3. c.
    board_games.sort('ascending')

Answer

B. board_games.sort()
The sort() function must be applied on the list. The default for the sort() function is to arrange elements in ascending order.

Concepts in Practice 9.2.5 §9.2 source ↗

Use the following list for the questions below.

board_games = ["go", "chess", "scrabble", "checkers"]
What is the correct way to reverse the list board_games?
  1. a.
    board_games.reverse()
  2. b.
    reverse(board_games)

Answer

A. board_games.reverse()
The correct syntax for the reverse() function requires the function to be applied to the list.

Concepts in Practice 9.2.6 §9.2 source ↗

Use the following list for the questions below.

board_games = ["go", "chess", "scrabble", "checkers"]
What would be the last element of board_games after the reverse() method has been applied?
  1. a.
    'go'
  2. b.
    'checkers'
  3. c.
    'scrabble'

Answer

A. 'go'
"go" is the first element of board_games, so "go" would be the last element once board_games is reversed.

Concepts in Practice 9.3.1 §9.3 source ↗

What is the correct way to get the minimum of a list named nums_list?
  1. a.
    min(nums_list)
  2. b.
    nums_list.min()
  3. c.
    minimum(nums_list)

Answer

A. min(nums_list)
The min() function takes a list as a parameter and returns the minimum for the list.

Concepts in Practice 9.3.2 §9.3 source ↗

What is the minimum of the following list?
["Lollapalooza", "Coachella", "Newport Jazz festival", "Hardly Strictly Bluegrass", "Austin City Limits"]
  1. a.
    Coachella
  2. b.
    Austin City Limits
  3. c.
    The minimum doesn't exist.

Answer

B. Austin City Limits
"Austin City Limits" is the first string in dictionary order.

Concepts in Practice 9.3.3 §9.3 source ↗

What value does the function call return?
sum([1.2, 2.1, 3.2, 5.9])
  1. a.
    sum() only works for integers.
  2. b.
    11
  3. c.
    12.4

Answer

C. 12.4
The summation of the given floats is 12.4 .

Concepts in Practice 9.3.4 §9.3 source ↗

What is the output of the following code?
my_list = [1, 2, 3]
list2 = my_list
list2[0] = 13
print(sum(my_list))
  1. a.
    6
  2. b.
    13
  3. c.
    18

Answer

C. 18
list2 refers to the same list as my_list, so any changes made to list2 also reflect on my_list. 18 is the summation of the values in the list.

Concepts in Practice 9.3.5 §9.3 source ↗

What is the output of the following code?
my_list = [1, 2, 3]
list2 = my_list.copy()
list2[0] = 13
print(max(my_list))
  1. a.
    3
  2. b.
    13
  3. c.
    18

Answer

A. 3
3 is the maximum value in the list my_list. Line 3 changes the value of the first element in list2.

Concepts in Practice 9.3.6 §9.3 source ↗

What is the output of the following code?
my_list = ["Cat", "Dog", "Hamster"]
list2 = my_list
list2[2] = "Pigeon"
print(sum(my_list))
  1. a.
    CatDogPigeon
  2. b.
    Error

Answer

B. Error
sum() function is not defined for string values.

Concepts in Practice 9.4.1 §9.4 source ↗

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


mat A= 7 4 5 3 9 6 1 2 8 What would be the correct way to represent matA in Python?
  1. a.
    [[7, 4, 5], [3, 9, 6], [1, 2, 8]]
  2. b.
    [7, 4, 5
    3, 9, 6
    1, 2, 8]
  3. c.
    [[7, 3, 1], [4, 9, 2], [1, 2, 8]

Answer

A.
[[7, 4, 5], [3, 9, 6], [1, 2, 8]]
Each row of the matrix is represented as a list in the larger list-of-lists, matA. matA represents the complete matrix.

Concepts in Practice 9.4.2 §9.4 source ↗

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


mat A= 7 4 5 3 9 6 1 2 8 What would be the correct index for the number 6 in the above list?
  1. a.
    [5]
  2. b.
    [2][1]
  3. c.
    [1][2]
Video · List-of-lists in the book ↗

Answer

C. [1][2]
6 is in the 2nd row and 3rd column. Using zero-indexing results in index 1 for the row and 2 for the column.

Concepts in Practice 9.4.3 §9.4 source ↗

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


mat A= 7 4 5 3 9 6 1 2 8 What would be the result of the following code:
print(matA[0])
  1. a.
    Error
  2. b.
    7
  3. c.
    [7, 4, 5]

Answer

C. [7, 4, 5]
The first element of the list matA is a list [7, 4, 5] representing the first row of the matrix.

Concepts in Practice 9.4.4 §9.4 source ↗

For each question below, consider the following list:

my_list = [[7, 4, 5, 12],
      [24, 3, 9, 16],
      [12, 8, 91, -5]]
Which code prints each number in my_list starting from 7, then 4, and so on ending with -5?
  1. a.
    for row in my_list:
      for elem in row:
        print(elem)
  2. b.
    for elem in my_list:
        print(elem)

Answer

A.
for row in my_list:
  for elem in row:
    print(elem)
The outer for loop iterates row by row. The inner for loop iterates through each element in a row. So all the numbers are printed, starting from 7 and ending in -5.

Concepts in Practice 9.4.5 §9.4 source ↗

For each question below, consider the following list:

my_list = [[7, 4, 5, 12],
      [24, 3, 9, 16],
      [12, 8, 91, -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. a.
    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. b.
    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. c.
    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])

Answer

C.
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])
The outer for loop runs for three iterations because there are three elements in the list-of-lists. The inner for loop iterates through each element of each row using the length of each row.

Concepts in Practice 9.5.1 §9.5 source ↗

The component of a list comprehension defining an element of the new list is the _____.
  1. a.
    expression
  2. b.
    loop_variable
  3. c.
    container

Answer

A. expression
The expression tells Python what element to put in the new list.

Concepts in Practice 9.5.2 §9.5 source ↗

What would be the contents of b_list after executing the code below?
a_list = [1, 2, 3, 4, 5]
b_list = [i+2 for i in a_list]
  1. a.
    [1, 2, 3, 4, 5]
  2. b.
    [0, 1, 2, 3, 4]
  3. c.
    [3, 4, 5, 6, 7]

Answer

C. [3, 4, 5, 6, 7]
i+2 results in adding 2 to each element of a_list to generate b_list.

Concepts in Practice 9.5.3 §9.5 source ↗

What does new_list contain after executing the statement below?
new_list = [i//3 for i in range(1, 15, 3)]
  1. a.
    [0.3333333333333333, 1.3333333333333333, 2.3333333333333335, 3.3333333333333335, 4.333333333333333]
  2. b.
    [0, 1, 2, 3, 4]
  3. c.
    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]

Answer

B. [0, 1, 2, 3, 4]
The for loop starts at 1 and steps by three, so it begins at 1 and ends at 13. Dividing using the // operator results in dropping the part after the decimal.

Concepts in Practice 9.5.4 §9.5 source ↗

For each code using list comprehension, select the correct resultant list in new_list.

my_list = [21, -1, 50, -9, 300, -50, 2]

new_list = [m for m in my_list if m < 0]
  1. a.
    [21, 50, 300, 2]
  2. b.
    [21, -1, 50, -9, 300, -50, 2]
  3. c.
    [-1, -9, -50]

Answer

C. [-1, -9, -50]
The list comprehension filters for negative numbers in my_list.

Concepts in Practice 9.5.5 §9.5 source ↗

For each code using list comprehension, select the correct resultant list in new_list.

my_string = "This is a home."

new_list = [i for i in my_string if i in 'aeiou']
  1. a.
    [i, i, a, o, e]
  2. b.
    ['i', 'i'', 'a', 'o', 'e']
  3. c.
    Error

Answer

B. ['i', 'i'', 'a', 'o', 'e']
The list comprehension selects vowels from the given string.

Concepts in Practice 9.5.6 §9.5 source ↗

For each code using list comprehension, select the correct resultant list in new_list.

new_list = [r for r in range (0, 21, 2) if r%2 != 0]
  1. a.
    []
  2. b.
    [21]
  3. c.
    [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

Answer

A. []
The for loop ranges from 0 through 20, stepping by two, but each iterated element is even, so nothing is added to new_list.

Loading chapter 10…

Problems and answers are adapted from Introduction to Python Programming by Udayan Das, Aubrey Lawson, Chris Mayfield and Narges Norouzi, OpenStax, and are used under the CC BY-NC-SA 4.0 licence. Access for free at https://openstax.org/books/introduction-python-programming/pages/1-introduction. Illustrations: Copyright Rice University, OpenStax, under CC BY-NC-SA 4.0 license. This adaptation is released under the same licence, is non-commercial, and is neither affiliated with nor endorsed by OpenStax or Rice University.