Every problem in the chapter beside the book’s answer, a chapter at a time as you scroll.
Chapter 9: Lists
29 problems · 0 figuresConcepts in Practice 9.1.1 §9.1 source ↗
- a.
add() - b.
append() - c.
pop()
Answer
append()append() operation adds an element to the end of a list.Concepts in Practice 9.1.2 §9.1 source ↗
23 from a list called number_list?- a.
remove() - b.
number_list.remove() - c.
number_list.remove(23)
Answer
number_list.remove(23)Concepts in Practice 9.1.3 §9.1 source ↗
- a.only
pop() - b.only
remove() - c.either
pop()orremove()
Answer
pop() or remove()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: - a.5
- b.4
Answer
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)):- a.
9 - b.
4 - c.
5
Answer
4len(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=' ')
- a.
2 5 9 - b.
2 3 5 7 9 - c.
2
5
9
Answer
2 5 9 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 ↗
[12, 3, 19, 25, 16, -3, 5]- a.
25 - b.
-3 - c.
5
Answer
-3Concepts in Practice 9.2.2 §9.2 source ↗
["cat", "bat", "dog", "coyote", "wolf"]- a.
["bat", "cat", "coyote", "dog", "wolf"] - b.
["wolf", "coyote", "dog", "cat", "bat"]
Answer
["bat", "cat", "coyote", "dog", "wolf"]Concepts in Practice 9.2.3 §9.2 source ↗
"flask" and "flash" related in Python?- a.
"flask" < "flash" - b.
"flask" == "flash" - c.
"flask" > "flash"
Answer
"flask" > "flash" "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?- a.
sort(board_games) - b.
board_games.sort() - c.
board_games.sort('ascending')
Answer
board_games.sort()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?- a.
board_games.reverse() - b.
reverse(board_games)
Answer
board_games.reverse()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?- a.
'go' - b.
'checkers' - c.
'scrabble'
Answer
'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 ↗
nums_list?- a.
min(nums_list) - b.
nums_list.min() - c.
minimum(nums_list)
Answer
min(nums_list)min() function takes a list as a parameter and returns the minimum for the list.Concepts in Practice 9.3.2 §9.3 source ↗
["Lollapalooza", "Coachella", "Newport Jazz festival", "Hardly Strictly Bluegrass", "Austin City Limits"]- a.
Coachella - b.
Austin City Limits - c.The minimum doesn't exist.
Answer
Austin City Limits "Austin City Limits" is the first string in dictionary order.Concepts in Practice 9.3.3 §9.3 source ↗
sum([1.2, 2.1, 3.2, 5.9])- a.
sum()only works for integers. - b.
11 - c.
12.4
Answer
12.4 12.4 .Concepts in Practice 9.3.4 §9.3 source ↗
my_list = [1, 2, 3] list2 = my_list list2[0] = 13 print(sum(my_list))
- a.
6 - b.
13 - c.
18
Answer
18list2 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 ↗
my_list = [1, 2, 3] list2 = my_list.copy() list2[0] = 13 print(max(my_list))
- a.
3 - b.
13 - c.
18
Answer
3my_list. Line 3 changes the value of the first element in list2.Concepts in Practice 9.3.6 §9.3 source ↗
my_list = ["Cat", "Dog", "Hamster"] list2 = my_list list2[2] = "Pigeon" print(sum(my_list))
- a.
CatDogPigeon - b.
Error
Answer
Errorsum() 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:
What would be the correct way to represent
matA in Python?- a.
[[7, 4, 5], [3, 9, 6], [1, 2, 8]]
- b.
[7, 4, 5 3, 9, 6 1, 2, 8]
- c.
[[7, 3, 1], [4, 9, 2], [1, 2, 8]
Answer
[[7, 4, 5], [3, 9, 6], [1, 2, 8]]
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:
What would be the correct index for the number
6 in the above list?- a.
[5] - b.
[2][1] - c.
[1][2]
Answer
[1][2]Concepts in Practice 9.4.3 §9.4 source ↗
For each of the questions below, consider the following matrix:
What would be the result of the following code:
print(matA[0])
- a.
Error - b.
7 - c.
[7, 4, 5]
Answer
[7, 4, 5]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?- a.
for row in my_list: for elem in row: print(elem) - b.
for elem in my_list: print(elem)
Answer
for row in my_list:
for elem in row:
print(elem) 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?- 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]) - 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() - 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
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]) 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 ↗
- a.expression
- b.loop_variable
- c.container
Answer
Concepts in Practice 9.5.2 §9.5 source ↗
b_list after executing the code below? a_list = [1, 2, 3, 4, 5] b_list = [i+2 for i in a_list]
- a.
[1, 2, 3, 4, 5] - b.
[0, 1, 2, 3, 4] - c.
[3, 4, 5, 6, 7]
Answer
[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 ↗
new_list contain after executing the statement below? new_list = [i//3 for i in range(1, 15, 3)]
- a.
[0.3333333333333333, 1.3333333333333333, 2.3333333333333335, 3.3333333333333335, 4.333333333333333] - b.
[0, 1, 2, 3, 4] - c.
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
Answer
[0, 1, 2, 3, 4] 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]
- a.
[21, 50, 300, 2] - b.
[21, -1, 50, -9, 300, -50, 2] - c.
[-1, -9, -50]
Answer
[-1, -9, -50]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']
- a.
[i, i, a, o, e] - b.
['i', 'i'', 'a', 'o', 'e'] - c.
Error
Answer
['i', 'i'', 'a', 'o', 'e']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]
- a.
[] - b.
[21] - c.
[1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
Answer
[] 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.