Creating and Initializing Lists
Lists are mutable: you can change their contents after creation by reassigning elements using bracket notation on the left side of an assignment.
A List That Can Change
A list is not limited to the contents it had when it was created. Lists are mutable, which means their contents can be changed after creation. The change happens when bracket notation selects an element on the left side of an assignment.
The assignment list[index] = value replaces the element selected by index in the existing list.
Reading Index Positions
A list represents a mapping between indices and elements. Each index position corresponds to exactly one element. Bracket notation selects the element associated with a particular index, so changing the index changes which element is selected.
selected refers to "green".Replacing One Element
Changing the middle element
Start with the list ["red", "green", "blue"] and replace the element at index 1 with "yellow".
Select the position: The expression colors[1] selects the element mapped to index 1.
Assign a new value: Writing colors[1] = "yellow" reassigns the selected element.
Read the resulting list: The first and third elements remain as they were, while the element at index 1 has the new value.
["red", "yellow", "blue"]
colors is now ["red", "yellow", "blue"].Mutability in Action
Mutability describes what can happen after creation: the contents of a list can be changed. Reassigning an indexed element is one direct way to perform that change. The list is still the existing list, but its contents now reflect the new value at the selected position.
When reading or changing a list, identify the index first. Then ask which element that index maps to before deciding whether the assignment selects the intended position.
Checking Membership
The in operator checks whether a value exists in a list. Its result is True when the value is present and False when the value is not present.
The first membership test produces True. The second membership test produces False.Membership testing asks whether a value is present; indexed assignment changes the value at a selected position. These operations answer different questions.
Practice and Pitfalls
What do you think happens?
What is the resulting list after this reassignment?
Reveal answer
Answer: ["small", "wide", "large"]
Index 1 selects the second element, "medium", so assigning "wide" replaces that element.
Assuming a list cannot change after creation.
Lists are mutable, so their contents can be changed after creation.
Fix:
Use bracket notation on the left side of an assignment when you need to reassign an element.Changing the wrong element because the index mapping was not checked.
Each index position corresponds to one particular element.
Fix:
Map the index to its element before assigning a new value.Confusing membership testing with reassignment.
The in operator checks whether a value exists and returns True or False.
Fix:
Use indexed assignment to change an element and use in to test membership.
Given the list ["Monday", "Tuesday", "Friday"], identify the element selected by index 1. Then describe the resulting list after reassigning index 1 to "Wednesday". Finally, state whether "Friday" in the list produces True or False.
Hints
- Start by matching index 1 with its element.
- Only the selected element is replaced by the assignment.
- The in operator produces True when the tested value exists in the list.
Key Takeaways
- Lists are mutable, so their contents can be changed after creation.
- A list maps index positions to elements, with each index corresponding to one element.
- Bracket notation on the left side of an assignment reassigns the selected element in the existing list.
- Reassignment changes the list in place rather than creating a new list.
- The in operator checks membership and returns True when a value exists or False when it does not.
Key Takeaways
- A list is mutable and can be modified after creation.
- Indices provide the mapping from positions to list elements.
- An assignment such as list[index] = value replaces the selected element in the existing list.
- The in operator checks whether a value is present and returns True or False.