0
0
Pythonprogramming~20 mins

List mutability in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
๐ŸŽ–๏ธ
List Mutability Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ“ Predict Output
intermediate
2:00remaining
What is the output of this list mutation?
Consider the following Python code. What will be printed after running it?
Python
lst = [1, 2, 3]
new_lst = lst
new_lst.append(4)
print(lst)
A[1, 2, 3]
BError
C[1, 2, 3, 4]
D[4]
Attempts:
2 left
๐Ÿ’ก Hint
Remember that lists are mutable and variables can point to the same list.
โ“ Predict Output
intermediate
2:00remaining
What is the output after modifying a copied list?
What will this code print?
Python
lst = [1, 2, 3]
copied = lst[:]
copied.append(4)
print(lst)
A[1, 2, 3]
B[1, 2, 3, 4]
C[4]
DError
Attempts:
2 left
๐Ÿ’ก Hint
Slicing a list creates a new list copy.
โ“ Predict Output
advanced
2:00remaining
What is the output of nested list mutation?
What will this code print?
Python
lst1 = [1, 2]
lst2 = [lst1, 3]
lst2[0].append(4)
print(lst1)
A[1, 2]
BError
C[4]
D[1, 2, 4]
Attempts:
2 left
๐Ÿ’ก Hint
The first element of lst2 is the same list as lst1.
โ“ Predict Output
advanced
2:00remaining
What error does this code raise?
What error will this code produce?
Python
lst = [1, 2, 3]
lst[3] = 4
print(lst)
ANo error, prints [1, 2, 3, 4]
BIndexError
CValueError
DTypeError
Attempts:
2 left
๐Ÿ’ก Hint
Check if the index 3 exists in the list before assignment.
๐Ÿง  Conceptual
expert
2:00remaining
How many items are in the list after these operations?
What is the length of 'lst' after running this code?
Python
lst = [1, 2, 3]
new_lst = lst
new_lst = new_lst + [4]
print(len(lst))
A3
B4
CError
D1
Attempts:
2 left
๐Ÿ’ก Hint
The '+' operator creates a new list instead of modifying in place.