Challenge - 5 Problems
List Mutability Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediateWhat 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)
Attempts:
2 left
๐ก Hint
Remember that lists are mutable and variables can point to the same list.
โ Incorrect
Both 'lst' and 'new_lst' point to the same list object. Appending 4 via 'new_lst' changes the original list, so printing 'lst' shows the updated list.
โ Predict Output
intermediateWhat 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)
Attempts:
2 left
๐ก Hint
Slicing a list creates a new list copy.
โ Incorrect
The 'copied' list is a new list separate from 'lst'. Appending 4 to 'copied' does not affect 'lst'.
โ Predict Output
advancedWhat 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)
Attempts:
2 left
๐ก Hint
The first element of lst2 is the same list as lst1.
โ Incorrect
lst2[0] points to lst1. Appending 4 to lst2[0] changes lst1 as well.
โ Predict Output
advancedWhat error does this code raise?
What error will this code produce?
Python
lst = [1, 2, 3] lst[3] = 4 print(lst)
Attempts:
2 left
๐ก Hint
Check if the index 3 exists in the list before assignment.
โ Incorrect
Index 3 is out of range for a list of length 3, so Python raises IndexError.
๐ง Conceptual
expertHow 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))
Attempts:
2 left
๐ก Hint
The '+' operator creates a new list instead of modifying in place.
โ Incorrect
The '+=' operator modifies the list in place, but '+' creates a new list assigned to new_lst only. 'lst' remains unchanged with length 3.
