0
0
Pythonprogramming~10 mins

List mutability in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - List mutability
Create list
Assign to variable
Modify list element
List content changes
Variable still points to same list
Use modified list
This flow shows how a list is created, modified in place, and the variable still points to the same list with updated content.
Execution Sample
Python
my_list = [1, 2, 3]
my_list[1] = 99
print(my_list)
This code creates a list, changes the second element, and prints the updated list.
Execution Table
StepActionList ContentVariable ReferenceOutput
1Create list [1, 2, 3][1, 2, 3]my_list -> [1, 2, 3]
2Modify element at index 1 to 99[1, 99, 3]my_list -> [1, 99, 3]
3Print list[1, 99, 3]my_list -> [1, 99, 3][1, 99, 3]
4End of program[1, 99, 3]my_list -> [1, 99, 3]Program ends
💡 Program ends after printing the modified list.
Variable Tracker
VariableStartAfter Step 2Final
my_list[1, 2, 3][1, 99, 3][1, 99, 3]
Key Moments - 3 Insights
Why does changing my_list[1] affect the original list?
Because lists are mutable, modifying an element changes the list in place. The variable still points to the same list object, so the change is visible.
Does modifying the list create a new list?
No, the list is changed in place. The variable still references the same list object, just with updated content (see Step 2 in execution_table).
What if we assign my_list = [4, 5, 6] instead of modifying an element?
Then my_list points to a new list object. The original list remains unchanged. This is different from mutating the list content.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 2, what is the list content after modification?
A[1, 2, 3]
B[1, 99, 3]
C[99, 2, 3]
D[1, 3, 99]
💡 Hint
Check the 'List Content' column at Step 2 in the execution_table.
At which step does the variable my_list point to a different list object?
AStep 3
BStep 2
CNone of the steps
DStep 1
💡 Hint
Look at the 'Variable Reference' column; my_list points to the same list throughout.
If we replaced my_list[1] = 99 with my_list = [4, 5, 6], what would happen to the original list?
AIt would remain [1, 2, 3]
BIt would be deleted
CIt would be modified to [4, 5, 6]
DIt would become empty
💡 Hint
Assigning a new list to my_list changes the variable's reference, not the original list object.
Concept Snapshot
List mutability means lists can be changed after creation.
Modify elements by index: my_list[1] = new_value.
The variable still points to the same list object.
Changes affect the original list in place.
Assigning a new list changes the variable's reference.
Full Transcript
This example shows how a list in Python can be changed after it is created. First, a list [1, 2, 3] is made and stored in the variable my_list. Then, the second element (index 1) is changed to 99. Because lists are mutable, this change updates the original list. The variable my_list still points to the same list, but now the list content is [1, 99, 3]. When we print my_list, it shows the updated list. This demonstrates that modifying a list element changes the list in place without creating a new list object.