Playing as a guest? to earn XP and track your progress.
๐ List Manipulation Quiz
๐ List Manipulation
A list in Python is written inside:
Round brackets ( )
Curly brackets { }
Double quotes " "
Square brackets [ ]
Example: marks = [45, 78, 92].
Which statement about lists is TRUE?
A list cannot have duplicate items
A list is ordered and mutable
A list can hold only numbers
A list is unordered and immutable
Lists keep their order, can be changed, allow duplicates and can mix types.
Can a list contain items of different data types, like [1, "two", 3.0]?
No, all items must be the same type
Only inside a tuple
Only numbers can be mixed
Yes
A list can hold items of any type.
Which statement creates an EMPTY list?
L = {}
L = ""
L = ()
L = []
[] (or list()) creates an empty list.
What is the output of:
print(list("abc"))['abc']
[a, b, c]
['a', 'b', 'c']
abc
list() on a string makes a list of its characters.
What is the output of:
print(list(range(4)))[0, 1, 2, 3]
[0, 1, 2, 3, 4]
[4]
[1, 2, 3, 4]
range(4) gives 0, 1, 2, 3.
L = [10, 20, 30, 40]. What is L[1]?
30
40
10
20
Indexing starts at 0, so index 1 is the second item.
L = [10, 20, 30, 40]. What is L[-1]?
10
40
Error
30
Negative index โ1 means the last item.
L = [10, 20, 30, 40]. What is L[-3]?
30
40
10
20
Counting from the right: โ1 is 40, โ2 is 30, โ3 is 20.
L = [10, 20, 30]. What does L[3] give?
IndexError
0
None
30
Valid indexes are 0, 1 and 2 only.
For a list of 6 items, what is the index of the LAST item?
-6
6
5
0
Indexes go from 0 to length โ 1.
What is the output of:
L = [1, 2, 3]
L[1] = 9
print(L)Error
[1, 2, 9]
[9, 2, 3]
[1, 9, 3]
Lists are mutable, so L[1] can be changed.
L = [5, 10, 15, 20, 25]. What is L[1:4]?
[10, 15, 20, 25]
[10, 15, 20]
[15, 20, 25]
[5, 10, 15, 20]
The slice includes the start index and stops before the stop index.
L = [5, 10, 15, 20, 25]. What is L[:2]?
[5, 10]
[15, 20, 25]
[5, 10, 15]
[10]
No start means from the beginning.
L = [5, 10, 15, 20, 25]. What is L[2:]?
[15, 20, 25]
[5, 10]
[20, 25]
[10, 15, 20, 25]
No stop means till the end.
L = [5, 10, 15, 20, 25]. What is L[::2]?
[25, 15, 5]
[10, 20]
[5, 10]
[5, 15, 25]
Step 2 takes every second item.
L = [1, 2, 3, 4]. What is L[::-1]?
[1, 2, 3, 4]
[4, 3, 2, 1]
[4]
Error
A step of โ1 gives the list in reverse.
L = [1, 2, 3]. What is L[1:10]?
[]
IndexError
[2, 3, None]
[2, 3]
Slicing never gives an IndexError; it stops at the end of the list.
L = [10, 20, 30, 40, 50]. What is L[-3:-1]?
[20, 30]
[40, 50]
[30, 40]
[30, 40, 50]
โ3 is 30 and โ1 is 50; the slice stops before 50.
What is the output of:
print([1, 2] + [3, 4])[4, 6]
[1, 2, 3, 4]
Error
[[1, 2], [3, 4]]
- joins (concatenates) two lists.
What is the output of:
print([0] * 3)0
[0, 0, 0]
[3]
[0]
* repeats the list.
What does [1, 2, 3] + 4 give?
10
[5, 6, 7]
[1, 2, 3, 4]
TypeError
Only a list can be joined to a list.
What is the output of:
print(3 in [1, 2, 3])True
Error
False
3
in checks whether the value is in the list.
What is the output of:
print(len([4, 5, [6, 7]]))4
Error
3
2
The inner list [6, 7] counts as ONE item.
What is the output of:
for x in [2, 4, 6]:
print(x * 2, end=" ")246246
4 8 12
2 4 6
12
Each item is doubled and printed.
Which loop visits every INDEX of list L?
for i in range(len(L)):
for i in range(L):
for i in L:
for i in len(L):
range(len(L)) gives 0 to len(L) โ 1.
What is the output of:
L = [1, 2]
L.append(3)
print(L)[1, 2, 3]
Error
[3, 1, 2]
[1, 2, [3]]
append() adds one item at the end.
What is the output of:
L = [1, 2]
L.append([3, 4])
print(L)[1, 2, 3, 4]
Error
[1, 2, [3, 4]]
[3, 4, 1, 2]
append() adds the whole list as ONE item.
What is the output of:
L = [1, 2]
L.extend([3, 4])
print(L)[1, 2, [3, 4]]
[3, 4, 1, 2]
Error
[1, 2, 3, 4]
extend() adds each item of the given list.
What is the output of:
L = [10, 30]
L.insert(1, 20)
print(L)[20, 10, 30]
[10, 30, 20]
[10, 20, 30]
[10, 1, 20, 30]
insert(1, 20) puts 20 at index 1.
Which method adds one item at a GIVEN position?
insert()
extend()
add()
append()
insert(index, item) places the item at that index.
What is the output of:
L = [5, 6, 7]
x = L.pop()
print(x, L)7 [5, 6, 7]
5 [6, 7]
7 [5, 6]
None [5, 6]
pop() without an index removes and returns the last item.
What is the output of:
L = [5, 6, 7]
L.pop(0)
print(L)[5, 7]
Error
[5, 6]
[6, 7]
pop(0) removes the item at index 0.
What is the output of:
L = [3, 1, 3, 2]
L.remove(3)
print(L)[1, 2]
[3, 1, 2]
[3, 3, 2]
[1, 3, 2]
remove() deletes only the FIRST occurrence of the value.
What does L.remove(9) do if 9 is not in L?
IndexError
Nothing
Removes the last item
ValueError
remove() raises ValueError if the value is not found.
Which statement removes the item at index 2 from list L?
L.remove(2)
L.clear(2)
L.delete(2)
del L[2]
del L[2] deletes by position; remove(2) would delete the VALUE 2.
What is the output of:
L = [1, 2, 3]
L.clear()
print(L)[]
None
Error
[1, 2, 3]
clear() removes all items, leaving an empty list.
pop() and remove() differ because:
pop() removes by position and returns the item; remove() removes by value
pop() deletes the whole list
remove() returns the item
Both remove by value
pop(i) uses an index; remove(x) uses a value.
What is the output of:
print([1, 2, 1, 1].count(1))2
3
4
1
count() tells how many times a value appears.
What is the output of:
print([4, 8, 8, 2].index(8))1
8
2
0
index() gives the position of the FIRST occurrence.
What is the output of:
L = [3, 1, 2]
L.sort()
print(L)[3, 1, 2]
None
[1, 2, 3]
[3, 2, 1]
sort() arranges the list in ascending order in place.
What is the output of:
L = [3, 1, 2]
L.sort(reverse=True)
print(L)Error
[3, 2, 1]
[1, 2, 3]
[2, 1, 3]
reverse=True sorts in descending order.
What is the output of:
L = [3, 1, 2]
print(L.sort())[1, 2, 3]
None
Error
[3, 1, 2]
sort() changes the list in place and returns None.
What is the output of:
L = [3, 1, 2]
M = sorted(L)
print(L, M)[3, 1, 2] [1, 2, 3]
[1, 2, 3] None
[1, 2, 3] [1, 2, 3]
[3, 1, 2] None
sorted() returns a NEW sorted list and leaves the original unchanged.
What is the output of:
L = [1, 5, 3]
L.reverse()
print(L)[1, 3, 5]
[5, 3, 1]
[3, 5, 1]
[1, 5, 3]
reverse() reverses the order of items; it does not sort them.
L = [4, 9, 2, 7]. What is max(L) + min(L)?
13
9
11
22
max is 9, min is 2, and 9 + 2 = 11.
L = [10, 20, 30]. What is sum(L) / len(L)?
60
20.0
3
20
Average = 60 / 3 = 20.0 (/ gives a float).
What is the output of:
print(max(["apple", "mango", "banana"]))Error
mango
apple
banana
Strings are compared alphabetically; m comes last.
A list that contains another list as an item is called a:
Dictionary
Slice
Tuple
Nested list
Example: [[1, 2], [3, 4]].
L = [[1, 2], [3, 4], [5, 6]]. What is L[1][0]?
1
2
4
3
L[1] is [3, 4], and its index 0 is 3.
L = [[1, 2], [3, 4], [5, 6]]. What is L[-1][-1]?
5
[5, 6]
6
1
The last inner list is [5, 6], and its last item is 6.
What is the output of:
L = [12, 45, 7, 30]
big = L[0]
for x in L:
if x > big:
big = x
print(big)7
12
30
45
The loop keeps the largest value seen so far.
What is the output of:
L = [3, 8, 5, 6]
c = 0
for x in L:
if x % 2 == 0:
c += 1
print(c)1
2
3
4
8 and 6 are even.
Searching for a value by checking every item one by one is called:
Binary search
Linear search
Sorting
Slicing
Linear search checks each element from the start until it finds the value.
What is the output of:
A = [1, 2]
B = A
B.append(3)
print(A)[1, 2]
[1, 2, 3]
Error
[3]
B = A does not copy the list; both names refer to the SAME list.
What is the output of:
print([1, 2, 3] == [1, 2, 3])Error
True
None
False
Lists with the same items in the same order are equal.
What is the output of:
print([1, 2] < [1, 3])False
None
True
Error
Lists are compared item by item; 2 < 3.
Which of these is NOT a list method?
append()
pop()
insert()
push()
Python lists have no push() method; use append().