Lists can be changed after they are created. This helps us update, add, or remove items easily.
List mutability in Python
Start learning this pattern below
Jump into concepts and practice - no test required
my_list = [1, 2, 3] my_list[0] = 10 # Change first item my_list.append(4) # Add new item my_list.remove(2) # Remove item with value 2
Lists use square brackets [] and can hold any type of items.
You can change items by their position (index) or use methods like append() and remove().
my_list = [] my_list.append(5) print(my_list)
my_list = [10] my_list[0] = 20 print(my_list)
my_list = [1, 2, 3] my_list[2] = 30 print(my_list)
my_list = [1, 2, 3] my_list.remove(2) print(my_list)
This program shows how to change, add, and remove items in a list. It prints the list before and after each change.
def print_list_state(description, some_list): print(f"{description}: {some_list}") my_list = [5, 10, 15] print_list_state("Original list", my_list) # Change the first item my_list[0] = 50 print_list_state("After changing first item", my_list) # Add a new item my_list.append(20) print_list_state("After adding new item", my_list) # Remove an item my_list.remove(10) print_list_state("After removing an item", my_list)
Changing an item by index is very fast (constant time).
Adding or removing items can take longer if the list is big.
Remember, if you assign a list to another variable, both variables point to the same list. Changing one changes the other.
Use list mutability when you want to update data without making a new list each time.
Lists can be changed after creation; this is called mutability.
You can update items by position, add new items, or remove items.
Mutability helps keep your program flexible and efficient.
Practice
Solution
Step 1: Understand list mutability
Python lists are mutable, meaning you can change their contents after creation.Step 2: Compare options
Lists can be changed after they are created. ("Lists can be changed after they are created.") correctly states that lists are mutable. Options A and C incorrectly claim lists cannot be changed or are immutable like strings. Lists can only store numbers. is wrong because lists can store any data type.Final Answer:
Lists can be changed after they are created. -> Option BQuick Check:
List mutability = True [OK]
- Confusing lists with strings (immutable)
- Thinking lists only hold numbers
- Believing lists cannot be changed
my_list to 10?Solution
Step 1: Recall list item assignment syntax
In Python, list items are accessed and assigned using square brackets and index, likemy_list[0].Step 2: Check each option
my_list[0] = 10 uses correct syntax. Options B and C use wrong brackets. my_list.set(0, 10) uses a method that does not exist for lists.Final Answer:
my_list[0] = 10 -> Option AQuick Check:
Use square brackets for list item assignment [OK]
- Using parentheses instead of brackets
- Trying to use curly braces for indexing
- Assuming lists have a set() method
numbers = [1, 2, 3] numbers[1] = 5 print(numbers)
Solution
Step 1: Understand list item update
The code changes the item at index 1 (second item) from 2 to 5.Step 2: Check the final list
After update, the list becomes [1, 5, 3]. The print statement outputs this list.Final Answer:
[1, 5, 3] -> Option AQuick Check:
Updated list = [1, 5, 3] [OK]
- Thinking index 1 is first item
- Expecting original list unchanged
- Assuming code causes error
my_list = [10, 20, 30] my_list(1) = 15 print(my_list)
Solution
Step 1: Identify indexing syntax error
The code uses parentheses()instead of square brackets[]to access list item.Step 2: Confirm correct syntax
List items must be accessed with square brackets, somy_list[1] = 15is correct.Final Answer:
Using parentheses instead of square brackets for indexing -> Option CQuick Check:
Use [] not () for list indexing [OK]
- Using () instead of [] for list indexing
- Thinking lists are immutable
- Looking for syntax errors elsewhere
data = [5, 10, 15, 20], which code snippet correctly doubles each item in the list using mutability?Solution
Step 1: Understand mutability and iteration
To change items in place, we must assign new values to each index in the list.Step 2: Analyze each option
for i in range(len(data)): data[i] = data[i] * 2 print(data) updates each item by index, correctly doubling values in place.
for item in data: item = item * 2 print(data) changes only the loop variable, not the list.
data = [item * 2 for item in data] print(data) creates a new list and assigns it to data (not in-place mutation).
data.map(lambda x: x*2) print(data) uses map which returns an iterator and does not modify list in place.Final Answer:
for i in range(len(data)): data[i] = data[i] * 2 print(data) -> Option DQuick Check:
In-place update requires index assignment [OK]
- Modifying loop variable instead of list items
- Using map without converting result
- Replacing list instead of mutating it
