Playing as a guest? to earn XP and track your progress.
๐ List Manipulation Quiz
๐ List Manipulation
Which statement best describes a Python list?
A sequence of characters only
An ordered, mutable collection of values in [ ]
An ordered, immutable collection in ( )
An unordered collection of key:value pairs
A list is an ordered, mutable collection of values (elements) in square brackets, separated by commas.
Which of these is a valid list containing values of different types?
(101, "Riya")
{101: "Riya"}
[101, "Riya", 91.5, True]
"101 Riya"
Lists are heterogeneous โ one list can hold int, str, float and bool values together.
What is list("HELLO")?
['H', 'E', 'L', 'L', 'O']
Error
['H', 'E', 'L', 'O']
['HELLO']
list() converts a sequence into a list of its items; a string gives its characters, and duplicates are kept.
Which statement lets the user type a whole list such as [10, 20, 30] at once?
L = input()
L = int(input())
L = list(input())
L = eval(input("Enter a list: "))
eval(input()) evaluates the typed text [10, 20, 30] as a list.
For L = [10, 20, 30, 40, 50], what does print(L[0], L[3], L[-1], L[-2]) display?
0 3 -1 -2
10 40 50 40
10 30 50 40
10 40 40 50
L[0] = 10, L[3] = 40, L[-1] = 50 and L[-2] = 40.
For L = [10, 20, 30, 40, 50], what does L[5] give?
50
0
IndexError: list index out of range
None
Valid indices are 0 to 4, so L[5] is out of range.
What is L after this code?
L = [10, 20, 30, 40, 50] L[1] = 200 L[-1] = 500
[10, 200, 30, 40, 500]
TypeError
[10, 20, 30, 40, 50]
[200, 20, 30, 40, 500]
Lists are mutable, so their elements can be changed in place.
Which statement comparing strings and lists is correct?
A string can hold values of any type
Lists cannot be sliced
Both are mutable
A list is mutable; a string is immutable
L[0] = 99 works on a list, but s[0] = 'X' gives a TypeError on a string. Indexing and slicing rules are the same for both.
What is the output of print([1, 2] * 2)?
[[1, 2], [1, 2]]
[1, 2, 1, 2]
[2, 4]
Error
- repeats the list.
What does [1, 2] + 3 give?
[1, 2, [3]]
[4, 5]
TypeError: can only concatenate list (not "int") to list
[1, 2, 3]
Both operands of + must be lists; write [1, 2] + [3].
For colours = ["red", "green", "blue"], what is "Green" in colours?
False
Error
1
True
Membership testing is case-sensitive; "Green" is not the same as "green".
What is the output of print([1, 2, 5] > [1, 2, 3])?
None
Error
False
True
Lists are compared element by element; at index 2, 5 > 3.
What is the output of print([3] > [1, 9, 9])?
False
True
None
Error
The first elements already differ (3 > 1), which decides the comparison.
For L = [11, 22, 33, 44, 55, 66, 77], what is L[1:4]?
[22, 33, 44, 55]
[11, 22, 33, 44]
[22, 33, 44]
[33, 44]
Indices 1, 2 and 3 are included; 4 is excluded.
For L = [11, 22, 33, 44, 55, 66, 77], what is L[5:1:-2]?
[66, 44]
[55, 33]
[]
[66, 44, 22]
Going backwards from index 5 in steps of 2: indices 5 and 3 (1 is excluded).
For L = [15, 25, 35, 45, 55, 65], what is L[-4:-1]?
[25, 35, 45]
[45, 55, 65]
[35, 45, 55, 65]
[35, 45, 55]
Index -4 is 35 and -1 (65) is excluded, giving 35, 45, 55.
For L = [15, 25, 35, 45, 55, 65], what is L[::-2]?
[15, 35, 55]
[65, 45, 25]
[25, 45, 65]
[65, 55, 45]
Start from the end and take every 2nd element going backwards.
For L = [15, 25, 35, 45, 55, 65], what is L[10:]?
None
IndexError
[] (an empty list)
[65]
Slicing beyond the end never raises an error; it returns an empty list.
What is L after this code?
L = [1, 2, 3, 4, 5] L[1:3] = [20, 30, 35]
[1, 20, 30, 35, 4, 5]
Error
[1, 20, 30, 4, 5]
[20, 30, 35, 4, 5]
The slice (indices 1-2) is replaced by three values, so the list grows.
What does this loop do to the list?
marks = [85, 92, 78] for m in marks: m = m + 5
Raises an error
Adds 5 to the first element only
Adds 5 to every element
Nothing โ m is only a temporary copy of each value
To change the elements, loop over the indices and assign to marks[i].
What is marks after this code?
marks = [85, 92, 78] for i in range(len(marks)): marks[i] = marks[i] + 5
Error
[90, 97, 83]
[85, 92, 78]
[5, 5, 5]
Assigning to marks[i] changes each element in place.
What is printed after this code?
A = [1, 2, 3] B = A B[0] = 100 print(A)
Error
[100]
[100, 2, 3]
[1, 2, 3]
B = A creates an alias โ both names refer to the same list, so A also changes.
Which of these does NOT create an independent copy of list A?
B = A
A[:]
list(A)
A.copy()
B = A only creates an alias; A.copy(), list(A) and A[:] create real copies.
What does this code print?
A = [1, 2, 3, 4] B = A C = A[:] B[0] = 100 C[1] = 200 print(A)
[100, 200, 3, 4]
[1, 2, 3, 4]
[1, 200, 3, 4]
[100, 2, 3, 4]
B is an alias of A, so A shows the change to index 0; C is a separate copy, so its change does not affect A.
For L = [4, 9, 1, 7], what are max(L), min(L) and sum(L)?
9, 4, 21
9, 1, 21
9, 1, 20
7, 1, 21
The largest element is 9, the smallest is 1, and the sum is 4 + 9 + 1 + 7 = 21.
For names = ["Riya", "Aman", "Zoya"], what is max(names)?
TypeError
'Riya'
'Zoya'
'Aman'
Strings are compared alphabetically, and 'Zoya' is the largest.
What happens with sum(["Riya", "Aman"])?
TypeError โ sum() cannot add strings
'RiyaAman'
2
0
sum() works on numbers only.
What is L after this code?
L = [1, 2, 3] L.append([4, 5])
Error
[1, 2, 3, 4, 5]
[[1, 2, 3], [4, 5]]
[1, 2, 3, [4, 5]]
append() adds its argument as ONE element, so the length becomes 4.
What is L after this code?
L = [1, 2, 3] L.extend([4, 5])
Error
[1, 2, 3, 4, 5]
[1, 2, 3, [4, 5]]
[4, 5, 1, 2, 3]
extend() adds each element of the sequence separately.
For L = [], what is the difference between L.append("hi") and L.extend("hi")?
Both give ['h', 'i']
Both give ['hi']
append gives ['hi']; extend gives ['h', 'i']
append gives ['h', 'i']; extend gives ['hi']
extend() adds each item of a sequence, and a string's items are its characters.
What is L after this code?
L = [1, 2, 3] L.insert(1, 10)
[1, 10, 2, 3]
[10, 1, 2, 3]
[1, 2, 3, 10]
[1, 2, 10, 3]
insert(i, x) puts x at index i and shifts the rest right.
What is printed after this code?
L = [1, 2, 3] L.insert(10, 4) L.insert(-1, 5) print(L)
[5, 1, 2, 3, 4]
IndexError
[1, 2, 3, 4, 5]
[1, 2, 3, 5, 4]
An index beyond the end adds at the end, giving [1, 2, 3, 4]; insert(-1, 5) then inserts before the last element.
What is L after running L = L.append(5)?
Error
None
[5]
The list with 5 added
append() changes the list in place and returns None, so L becomes None. Just write L.append(5).
For L = [10, 20, 30, 20], what do x = L.pop() and L become?
x = 10, L = [20, 30, 20]
x = 20, L = [10, 30, 20]
x = 20, L = [10, 20, 30]
x = None, L = [10, 30, 20]
pop() with no index removes and returns the last element.
For L = [10, 20, 30, 20], what is L after L.remove(20)?
[10, 30, 20]
[10, 20, 30]
[10, 30]
Error
remove() deletes only the FIRST occurrence of the value.
For L = [5, 8, 3], what does L.remove(9) give?
[5, 8, 3]
IndexError
None
ValueError: list.remove(x): x not in list
remove() raises a ValueError when the value is not in the list.
For L = [5, 8, 3], what does L.pop(7) give?
ValueError
IndexError: pop index out of range
None
3
pop() with an invalid index raises an IndexError.
Which statement comparing pop() and remove() is correct?
Both return the removed value
Both remove every occurrence
pop() removes by index and returns the value; remove() removes the first occurrence of a value and returns None
pop() removes by value; remove() removes by index
pop(i) works by index and returns the element; remove(x) works by value and returns None.
For L = [10, 20, 30, 20], what is L after del L[1:3]?
[10, 20]
[20, 30]
[10]
[10, 30, 20]
del L[1:3] deletes indices 1 and 2 (20 and 30).
What does L.clear() do?
Deletes the variable L
Removes the last element
Sorts the list
Removes all elements, leaving []
clear() empties the list, while del L deletes the variable itself.
For L = [3, 7, 3, 9, 3], what are L.index(9) and L.count(3)?
3 and 2
3 and 3
4 and 3
9 and 3
9 is first found at index 3, and 3 occurs three times.
What happens when you call L.find(5) on a list?
It returns -1
It returns the index of 5
An error โ lists have no find() method
It returns True
find() belongs to strings only; use L.index(x) for lists.
For L = [4, 1, 3], what is L after L.reverse()?
[3, 1, 4]
[1, 3, 4]
[4, 3, 1]
[1, 4, 3]
reverse() only flips the current order; it does not sort.
How do you sort list L in descending order, in place?
L.sort(-1)
L.reverse()
sorted(L)
L.sort(reverse=True)
L.sort(reverse=True) sorts in descending order in place.
What is printed after this code?
L = [40, 10, 30, 20] M = sorted(L) print(L)
[40, 30, 20, 10]
[40, 10, 30, 20]
None
[10, 20, 30, 40]
sorted() returns a NEW sorted list and leaves L unchanged.
Which statement comparing sort() and sorted() is correct?
sort() returns a new list; sorted() changes the original
Both change the original list
sort() sorts in place and returns None; sorted() returns a new sorted list
sorted() works only on lists
sort() is a list method that works in place; sorted() is a built-in function that works on any sequence.
What is words after this code?
words = ["banana", "Apple", "cherry"] words.sort()
['Apple', 'banana', 'cherry']
['apple', 'banana', 'cherry']
['cherry', 'banana', 'Apple']
['banana', 'cherry', 'Apple']
'A' (65) comes before lowercase letters such as 'b' (98).
For M = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], what is M[1][2]?
8
5
2
6
Row 1 is [4, 5, 6], and column 2 of that row is 6.
For M = [[1, 2, 3], [4, 5, 6], [7, 8, 9]], what is len(M)?
9
3
6
1
len(M) counts the rows (the inner lists), so it is 3.
What does len([1, [2, 3], 4]) return?
2
4
3
5
The nested list [2, 3] counts as ONE element.
What does the max/min/mean program print as the mean for L = [45, 12, 78, 34, 89, 23] (rounded to 2 decimal places)?
46.83
46.0
281
47.5
The total is 281 and 281 / 6 = 46.833โฆ, which rounds to 46.83.
In the max/min program, why are largest and smallest set to L[0] at the start?
To sort the list
Because L[0] is always the largest
Python requires it
To start comparing from an actual element of the list
Initialising both with the first element gives a valid starting point for the comparisons.
How does linear search work?
It works only on sorted lists
It compares the key with each element from the start until a match is found
It checks the middle element first
It sorts the list first
Linear search checks elements one by one; it works on unsorted lists, and in the worst case it checks all n elements.
In the linear-search program for L = [34, 7, 23, 32, 5, 62], what is printed for the key 32?
32 found at index 4
3
32 found at index 3
32 not found in the list
32 is at index 3 (34 is at 0, 7 at 1, 23 at 2).
In the linear-search program, when does the for loop's else part print 'not found'?
When the loop finishes without break, i.e. the key was not found
When a match is found
Never
Always
break runs only when the key is found; if it never runs, the loop's else executes.
What does this remove-duplicates program print for L = [3, 1, 3, 2, 1, 4]?
for x in L: if x not in unique: unique.append(x)
[1, 2, 3, 4]
[3, 3, 1, 1]
[4, 2, 1, 3]
[3, 1, 2, 4]
Each value is added the first time it appears, so the order is kept.
What is L after this code?
L = [1, 2, 3, 4, 5, 6] mid = len(L) // 2 L = L[mid:] + L[:mid]
[3, 2, 1, 6, 5, 4]
[4, 5, 6, 1, 2, 3]
[6, 5, 4, 3, 2, 1]
[1, 2, 3, 4, 5, 6]
mid = 3; the second half [4, 5, 6] is placed before the first half [1, 2, 3].
What is printed after this code?
L = [3, 8, 1, 6, 8, 2] L.remove(8) x = L.pop(1) L.insert(2, x * 2) print(L)
[3, 6, 8, 2]
[3, 1, 2, 8, 2]
[3, 6, 2, 8, 2]
[3, 2, 6, 8, 2]
remove(8) gives [3, 1, 6, 8, 2]; pop(1) removes 1, giving [3, 6, 8, 2]; insert(2, 2) gives [3, 6, 2, 8, 2].