Font size
WorksheetsPython List Operations 2
Total questions: 100
Worksheet time: 53mins
What is the output of the following code?
L = [10, 20, 30, 40, 50]
result = L[-4:4:2]
print(result)
[20, 40]
[20, 30, 40]
[30]
[20]
What is the result of len(L) after the following operations?
L = [1, 2] * 3
L = L + [4, 5]
6
7
8
5
What is the output? Python ?
L = [1, 2, 3]
L[1:3] = [9, 8, 7, 6]
print(L)
[1, 9, 8, 7, 6]
[1, 9, 8, 7, 6, 4]
[1, 9, 8, 7, 6, 3]
Error
What is the output?
L = ['a', 'b', 'c', 'd']
L[-1] = L[0]
print(L)
['a', 'b', 'c', 'a']
['d', 'b', 'c', 'a']
['d', 'b', 'c', 'd']
['a', 'b', 'c', 'd']
L = [10, 20, 30, 40]
What is the output of L[-1] + L[-2]?
70
60
50
30
L = ['a', 'b', ['x', 'y', 'z']]
What is the result of 'z' in L?
True
False
Error
Depends on Python version
What is the output?
L = [10, 20, 30, 40]
print(L[-2:])
[30, 40]
[20, 30, 40]
[40]
[10, 20]
What is the output?
L = [1, 2, 3, 4]
L[0:0] = [99]
print(L)
A: [99, 1, 2, 3, 4]
B: [1, 2, 3, 4]
C: [ ]
D: [99]
L = [1, 2, 3, 4, 5]
What is the value of sum(L) + max(L)?
15
20
18
25
What is the output?
L = [1, 2, 3]
del L[::2]
print(L)
A: [2]
B: [2, 4]
C: [1, 3]
D: Error
What is the final value of result?
L = [10, 20, 30, 40, 50, 60]
result = L[1:5][::-2]
[20, 40]
[60, 40, 20]
[50, 30]
[10, 30, 50]
Which operation results in a list containing [5, 5, 5, 5, 5]?
[5] * 4
[5 + 5 + 5 + 5 + 5]
list(5, 5, 5, 5, 5)
[5] + [5] * 3
What is the calculated value of result?
L = [1, 2, 3, 4, 5]
result = L[1::2][0] * L[::-1][1]
6
8
10
Error
What is the output?
L = [1, 2, 3, 4, 5]
result = L[2:2] + L[3:2:-1] + L[5:5]
print(result)
[]
[4]
[3,4]
4
Which of the following will create a shallow copy of L?
L2 = L
L2 = L.copy()
L2 = list(L)
Both B and C
What is the output?
L = [1, 2, 3]
L[1] = L[1] + 5
print(L)
[1, 7, 3]
[1, 2, 3, 5]
[7]
Error
What is the final value of L after execution?
L = [1, 2, 3]
L = [x * 2 for x in L]
L.pop(1)
L.insert(1, 7)
[2, 4, 6, 7]
[2, 6]
[2, 7, 6]
[1, 7, 3]
What is the output?
L = ['a', 'b']
L = L * 2
print(L)
['a', 'b', 'a', 'b']
['a', 'b']
['aa', 'bb']
['a', 'b'] * 2
What is the final value of L?
100
[99, 2]
[100]
[99,100]
Which statement is true about lists in Python?
Lists can only hold elements of the same data type.
Lists are immutable.
List indexing always starts from 1.
Lists are heterogeneous and mutable.
What is the output?
L = list(range(5))
result = L[::3]
print(result)
[0, 3]
[0, 1, 2, 3, 4]
[0, 4]
[3, 0]
What is the output?
L = [10, 20, 30]
L[2:] = []
print(L)
[10, 20, []]
[10, 20, 30]
[30]
What is the output ?
[]
Other
Index
Error
What is the output?
L = [10, 20]
L += (30, 40)
print(L)
[10, 20, 30, 40]
[10, 20, (30, 40)]
Error, cannot add list and tuple.
[10, 20, 70]
What is the output?
L = [10, 20, 30]
result = L[1:-1]
[20, 30]
[10, 20]
[20]
[]
What is the output?
L = [1, 2, 3, 4, 5]
L[4:1:-1] = [9, 8, 7]
print(L)
[1, 2, 7, 8, 9]
[1,2, 9, 8, 7]
Error: slice assignment cannot be used with step.
[1, 7, 8, 9, 5]
What is the output of L?
T = (1, 2, [3, 4])
L = list(T)
L[2].append(5)
print(L)
[1, 2, [3, 4]]
(1, 2, [3, 4, 5])
[1, 2, [3, 4, 5]]
Error as Tuple immutable
If L = [1, 2, 3], what is L * 0?
[0, 0, 0]
[1, 2, 3]
[]
Error
What is the result of L.count(1)?
L = [1, [1], 1, [1, 1]]
1
2
3
4
What is the final value of L?
L = [1, 2, 3, 4]
L[::] = sorted(L, reverse=True)
print(L)
[3, 2, 1]
[4, 3, 2, 1]
[4, 3, 2]
[]
Find the output?
[1, 2, 3, 4, 5]
[2, 3, 4, 5, 6]
15
[1, 2, 3, 4, 5, 1, 1, 1, 1, 1]
Which slice reverses the list L?
L[:-1]
L[:1:-1]
L[::-1]
L.reverse()
L = [1, 2, 3]
del L[1]
print(L)
[1, 3]
[2, 3]
[1, 2]
[3]
L = [5, 'a']
What is the result of min(L)?
5
'a'
'5'
Error (unorderable types)
Which statement about list traversal is incorrect?
You can use a for loop to iterate over elements.
You can use a while loop with an index to traverse.
A for loop is generally faster than a while loop for simple traversal.
The for i in L: loop provides access to the index of each element.
L = [10, 20]
L *= -1
print(L)
[10, 20, 10, 20]
[10, 20, -10, -20]
Error
[]
What is the output?
L = [10, 20, 30, 40]
del L[1::2]
print(L)
[10, 20, 30]
[10, 30]
[20, 40]
[10, 40]
List=[1,2]
K=List.append(3)
What is the return value of the append() method?
[1, 2, 3]
3
None
[1,2]
What is the output?
L = [1, 2]
L.append([3, 4])
print(len(L))
4
3
2
Error
What is the difference between L.append(x) and L.extend(x)?
append adds one element; extend adds multiple elements.
append modifies the original list; extend returns a new list.
append adds the element as a single item; extend adds elements of the iterable separately.
extend only works with other lists; append works with any data type.
What is the output?
L = [1, 2]
L.extend('abc')
print(L)
[1, 2, 'abc']
[1, 2, ['a', 'b', 'c']]
[1, 2, 'a', 'b', 'c']
Error
Q40: What is the output of the following Python code?
L = [1, 2, 3]
L.insert(10, 4)
print(L)
[1, 2, 3]
[1,2,3,0,4]
[1, 2, 3, 4]
Error
What is the value of L1[0] + L2[0]?
5
6
4
Error
What is the output ?
L = [10, 20, 30]
result = L.pop()
print(result, L)
A: 30 [10, 20]
B: [10, 20] 30
C: None [10, 20]
D: Error
What happens if L.pop(10) is called on a list of size 3?
It removes the last element.
It returns None.
It raises an IndexError.
It removes the first element.
What is the output?
L = [1, 2, 3, 2]
L.remove(2)
print(L)
[1, 3, 2]
[1, 3]
[1, 2, 3]
[1,2,2]
What is the return value of L.remove(2)?
The element removed (2)
The modified list
None
Error
What is the output?
L = [10, 20, 30]
L=L.reverse()
print(L)
[30, 20, 10]
[10, 20, 30]
None
Error
What is the difference between L.reverse() and L[::-1]?
reverse() modifies the list in-place; L[::-1] returns a new reversed list.
reverse() is for strings; L[::-1] is for lists.
reverse() returns a new list; L[::-1] modifies the list in-place.
No difference, they are equivalent.
What is the output?
[1, 2, 3]
None
None
None
Error
None
[1,2,3]
What is the difference between L.sort() and sorted(L)?
sort() returns a new sorted list; sorted() modifies the list in-place.
sort() works on lists only; sorted() works on any iterable.
sort() creates a shallow copy; sorted() creates a deep copy.
sorted() is faster than sort().
What is the output?
L = [30, 10, 20]
result = sorted(L, reverse=True)
print(result)
[30, 20, 10]
[10, 20, 30]
None
[30, 10, 20]
What is the output?
L = [1, 2, 3, 4]
L.insert(2, L.pop(1))
print(L)
[1, 3, 2, 4]
[1, 2, 3, 4]
[1, 2, 2, 3, 4]
Error
What is the output?
L = [10, 20, 30, 40]
L[1:3] = [L.pop(0)]
print(L)
[10,20, 10]
[10, 10]
[20, 10]
[10, 10,30,40]
What is the output?
L = list(range(10))
result = L[1:100:10]
print(result)
[1]
[1,10]
[]
[1,9]
What is the output ?
L = [10, 20, 30, 20, 50]
L.insert(L.index(20, 2),40)
print(L)
[10, 20, 30, 20, 50,40]
[10, 20, 30, 40, 50]
[10, 20, 30, 40, 20, 50]
D: Error
If L.count(x) returns 0, which statement is guaranteed to raise a ValueError?
L.pop(x)
L.remove(x)
L.index(x)
L.insert(0, x)
What is the output?
L = [10, 20, 30]
L.append(L.pop(0))
print(L)
[10, 20, 30]
[20, 30, 10]
[20, 30]
[30, 20, 10]
Which of the following correctly appends a tuple (1, 2) as a single element?
L.extend((1, 2))
L.append(1, 2)
L.append((1, 2))
L + (1, 2)
What is the output?
L = [1, 2, 3]
L[::2] = [10, 30]
print(L)
[10, 2, 30]
[10, 2, 30, 4]
Error: must assign iterable of same length.
[1, 2, 3, 10, 30]
What is the output?
L = [1, 2, 3]
L.insert(-1, 9)
print(L)
[9, 1, 2, 3]
[1, 9, 2, 3]
[1, 2, 9, 3]
[1, 2, 3, 9]
Which method is used to remove an item by its value?
pop()
index()
remove()
del
What is the output of L.pop(-1)?
L = [10, 20, 30]
10
20
30
Error
What is the output?
L = [1, 2, 3]
L[3:3] = [4, 5]
print(L)
A: [1, 2, 3, 4, 5]
B: Error (Index out of range)
C: [4, 5, 1, 2, 3]
D: [1, 2, 3, [4, 5]]
What does list(S) do, where S = 'Python'?
Creates a list ['P', 'y', 't', 'h', 'o', 'n'].
Creates a list ['Python'].
Creates a list of ASCII values.
Raises an error.
What is the output?
L = [1, 2, 3]
L[::2] = [9]
print(L)
[1,2,9]
[9, 2, 9]
[1, 9, 9]
ValueError:
How can you empty a list L most efficiently?
L.clear()
L = []
del L[:]
All of the above
What is the output?
[1,7,3]
[2,4,7,6]
[2,7,6]
[1,2,3,1]
Which method returns the number of times a value appears?
index()
count()
len()
sum()
What is the output?
[1, 2, 3]
[1, 2, 3, 0]
[3, 1, 2]
Error
What is the output?
L = [1, 2, 3, 4, 5]
result = L[2:2] + L[3:2:-1] + L[4:4]
print(result)
11
[11]
[4]
4
What is the output of the following Python code?
L = [1, 2, 3]
L = L.reverse()
print(L)
[3, 2, 1]
[1, 2, 3]
None
Error
What is the output of min(L)?
L = [1, [2, 3], 4]
A: 1
B: 2
C: [2, 3]
D: Error
What is the output?
L = [10, 20, 30]
result = (L.append(40) is None) and (L.pop(0) == 10)
print(result)
[20, 30, 40]
True
False
Error
What is the output?
L = [1, [2, 3], 4]
L[1][0] = 99
print(L)
[1, [99, 3], 4]
[1, 99, 4]
Error
[1, [2, 3], 4]
What is the output?
L = [1, 2, 3]
result = L[::2][::-1]
print(result)
[1,3]
[1, 2]
[1, 2, 3]
[3, 1]
What is the output?
L = [1, 2, 3, 4, 5, 6]
result = L[1::2][-1::-1][::2]
print(result)
[6, 4, 2]
[6, 2]
[6, 4]
[5, 3]
What is the output?
L = [1, 2, 3, 4, 5]
L[1:4] = L[4:1:-1]
print(L)
[1, 5, 4, 3, 5]
[1, 5, 4, 3, 5, 6]
[1, 5, 4, 3,6]
Error: target slice length must match assignment length.
Find the output?
31
21
36
26
What is the output?
L = [[1], 2, 3]
L2 = L.copy()
L2[0][0] = 99
print(L)
[[1], 2, 3]
[[99], 2, 3]
[[99], 1, 2]
[[1], 99, 3]
What is the output?
[1, 2, [3, 4]]
[1, 2, [3, 4, 5]]
[1, 99, [3, 4]]
[1, 99, [3, 4, 5]]
What is the output?
L = [1, 2, 3]
L[4:4] = [5]
print(L)
[1,2,5]
[1,2,3,4,4,4,4]
[1,2,3,5]
Error
What is the result of L[-10:] when L is a list of 5 elements?
Error
The last element
[]
The whole list
What is the final value of L1?
[1, 2, 3]
[1, 2, 3, 4]
None
Error
What is the final value of L1?
[1, 2, 3]
[5, 6, 7]
None
Error
What is the final value of L1?
[1, 2, 3]
[99, 2, 3]
[100]
[99, 100]
What is the output?
L = [1, 2, 3]
L[2::-2] = [9, 8]
print(L)
[5, 1, 2, 3]
[1, 2, 3, 5]
[8, 2, 9]
[1, 2, 3, 4, 5]
What is the output?
L = [1, 2, 3, 4]
result = L[:0]
print(result)
[]
[1, 2, 3, 4]
Error
[1]
What is the output?
[1, 2, 3, 4, 1, 2, 3, 4]
[2, 3, 4, 1, 2, 3, 4]
[1, 2, 3, 4, 2, 3, 4]
[2, 3, 4, 2, 3, 4]
What is the output?
L = [1, 2, 3, 4, 5]
del L[L[0] : L[3]]
print(L)
[]
[5]
[2,3,4]
[1, 5]
L = [1, 2, 3, 4, 5]
Which slice expression results in [3, 2]?
L[2::-1][1:3]
L[2:0:-1]
L[2:0:-2]
L[2:1:-1]
What is the output?
L = [1, 2]
L[1:1] = L
print(L)
[1, 1, 2, 2]
[1, 2, 1, 2]
[1, 2]
Error
What is the output?
L = [1, 2, 3, 4, 5]
result = L[L[1] + 1 : L[2] + 2]
print(result)
[3, 4, 5]
[3, 4]
[4, 5]
[2, 3, 4]
What is the final calculated value of result ?
L = [1, 2, 3, 4, 5]
result = L[1::2][0] * L[::-1][1]
6
8
10
12
What is the output?
L = [1, 2, 3]
L2 = L.copy()
L.remove(2)
print(L2)
[2, 3]
[1, 2, 3]
[1, 3]
[1,2,1,2]
What is the output?
L = [10, 20]
L.insert(-1, [30, 40])
print(L)
[10, [30, 40], 20]
[[30, 40], 10, 20]
[10, 30, 40, 20]
[10, 20, [30, 40]]
What is the output?
L = [1, 2, 3, 4]
del L[::2]
print(L)
[4, 2]
[1, 3]
[2, 4]
Error
What is the output?
L = [[1, 2], 3]
L2 = list(L)
L2[0].append(4)
print(L)
[[1, 2], 3]
[[1, 2, 4], 3]
[[1, 2, 4], 3]
Error
What is the final value of L?
[1, 2, 3]
[100]
[2, 2, 3]
[2, 100]
What is the value of L1?
[[1, 2], [3, 4]]
[[99], [3, 4, 5]]
[[1, 2, 5], [3, 4]]
[[1, 2], [3, 4, 5]]
