Lesson 12: Accessing and Modifying Lists
Learn how to modify Python lists: change items, add with .append() and .insert(), remove with .remove(), .pop(), del, and avoid common mistakes.
Introduction
In the last lesson, you learned how to create lists and access items by index. But lists would be pretty limited if you could only read from them.
The real power of lists comes from being able to change them. Add new items. Remove outdated ones. Update values as your project evolves.
This is what makes lists useful for real workflows. Your room schedule changes? Update the list. Project adds three floors? Add them to the list. Client removes a building wing? Remove those items.
Lists are mutable — they can be modified after creation. This lesson shows you how.
Lesson Overview
This section contains a general overview of topics you will learn in this lesson.
- Changing individual items in a list
- Adding items to a list (.append(), .insert())
- Removing items from a list (.remove(), .pop(), del)
- When to use each method
- Common mistakes when modifying lists
Changing Items
You can change any item in a list by assigning a new value to its index.
rooms = ["Office 1", "Office 2", "Conference Room"]
# Change the first room
rooms[0] = "Manager Office"
print(rooms)
# Output: ["Manager Office", "Office 2", "Conference Room"]
The syntax is the same as variable assignment, just with an index.
Architectural Example: Updating Sheet Names
sheets = ["A-101 Draft", "A-102 Draft", "A-201 Draft"]
# First sheet is now issued
sheets[0] = "A-101 Issued"
print(sheets)
# Output: ["A-101 Issued", "A-102 Draft", "A-201 Draft"]
You can change multiple items:
sheets[1] = "A-102 Issued"
sheets[2] = "A-201 Issued"
print(sheets)
# Output: ["A-101 Issued", "A-102 Issued", "A-201 Issued"]
Adding Items
There are several ways to add items to a list.
.append() — Add to the End
The most common way to add an item is with .append(). This adds the item to the end of the list.
rooms = ["Office 1", "Office 2"]
rooms.append("Conference Room")
print(rooms)
# Output: ["Office 1", "Office 2", "Conference Room"]
Architectural Example: Building a Sheet List
sheets = []
# Add sheets one by one
sheets.append("A-101")
sheets.append("A-102")
sheets.append("A-201")
print(sheets)
# Output: ["A-101", "A-102", "A-201"]
This is particularly useful when you're building a list dynamically — you start with an empty list and add items as you process data.
.insert() — Add at a Specific Position
If you need to add an item at a specific position (not the end), use .insert().
rooms = ["Office 1", "Office 2", "Office 3"]
# Insert at index 1 (second position)
rooms.insert(1, "Conference Room")
print(rooms)
# Output: ["Office 1", "Conference Room", "Office 2", "Office 3"]
The syntax is .insert(index, item). The new item goes at that index, and everything after it shifts to the right.
Architectural Example: Adding a Floor
floors = ["Ground", "First", "Second"]
# Add basement at the beginning
floors.insert(0, "Basement")
print(floors)
# Output: ["Basement", "Ground", "First", "Second"]
.extend() — Add Multiple Items
If you want to add multiple items at once, use .extend() with another list.
arch_sheets = ["A-101", "A-102"]
struct_sheets = ["S-101", "S-102"]
arch_sheets.extend(struct_sheets)
print(arch_sheets)
# Output: ["A-101", "A-102", "S-101", "S-102"]
This adds all items from the second list to the first list.
Note: This is different from .append():
# Using append (adds the list as a single item)
list_1 = ["A-101", "A-102"]
list_2 = ["S-101", "S-102"]
list_1.append(list_2)
print(list_1)
# Output: ["A-101", "A-102", ["S-101", "S-102"]]
# Using extend (adds each item from the list)
list_1 = ["A-101", "A-102"]
list_2 = ["S-101", "S-102"]
list_1.extend(list_2)
print(list_1)
# Output: ["A-101", "A-102", "S-101", "S-102"]
For combining lists, .extend() is usually what you want.
Removing Items
There are several ways to remove items from a list.
.remove() — Remove by Value
Use .remove() to delete the first occurrence of a specific value.
rooms = ["Office 1", "Office 2", "Conference Room", "Office 3"]
rooms.remove("Conference Room")
print(rooms)
# Output: ["Office 1", "Office 2", "Office 3"]
If the value doesn't exist, you get an error:
rooms.remove("Nonexistent Room")
# Error: ValueError: list.remove(x): x not in list
You can check first using in:
if "Conference Room" in rooms:
rooms.remove("Conference Room")
.pop() — Remove by Index
Use .pop() to remove an item at a specific index. It removes the item AND returns it.
sheets = ["A-101", "A-102", "A-201", "A-202"]
# Remove and get the last item
last_sheet = sheets.pop()
print(last_sheet) # Output: A-202
print(sheets) # Output: ["A-101", "A-102", "A-201"]
You can specify an index:
sheets = ["A-101", "A-102", "A-201", "A-202"]
# Remove second item (index 1)
removed = sheets.pop(1)
print(removed) # Output: A-102
print(sheets) # Output: ["A-101", "A-201", "A-202"]
If you don't specify an index, .pop() removes the last item.
del — Delete by Index
The del keyword removes an item at a specific index, but doesn't return it.
rooms = ["Office 1", "Office 2", "Conference Room"]
del rooms[1]
print(rooms)
# Output: ["Office 1", "Conference Room"]
You can also delete entire ranges:
sheets = ["A-101", "A-102", "A-201", "A-202", "S-101"]
# Delete items at index 2 and 3
del sheets[2:4]
print(sheets)
# Output: ["A-101", "A-102", "S-101"]
.clear() — Remove Everything
Use .clear() to remove all items from a list, leaving it empty.
rooms = ["Office 1", "Office 2", "Office 3"]
rooms.clear()
print(rooms)
# Output: []
When to Use Each Method
Adding items:
- Use
.append()when adding to the end (most common) - Use
.insert()when you need a specific position - Use
.extend()when adding multiple items from another list
Removing items:
- Use
.remove()when you know the value but not the position - Use
.pop()when you know the position and want the value back - Use
delwhen you know the position and don't need the value - Use
.clear()when you want to empty the entire list
Real Architectural Examples
Building a Drawing Set Dynamically
sheets = []
# Add architectural sheets
sheets.append("A-101")
sheets.append("A-102")
sheets.append("A-201")
# Add structural sheets
struct_sheets = ["S-101", "S-102"]
sheets.extend(struct_sheets)
print(sheets)
# Output: ["A-101", "A-102", "A-201", "S-101", "S-102"]
# Client removes second floor
sheets.remove("A-201")
print(sheets)
# Output: ["A-101", "A-102", "S-101", "S-102"]
Managing Room Schedules
rooms = ["Office 1", "Office 2", "Conference Room"]
# Scope change: add break room
rooms.append("Break Room")
# Rename first office
rooms[0] = "Manager Office"
# Remove conference room (not needed)
rooms.remove("Conference Room")
print(rooms)
# Output: ["Manager Office", "Office 2", "Break Room"]
Processing Floor Lists
floors = ["Basement", "Ground", "First", "Second"]
# Add penthouse
floors.append("Penthouse")
# Remove basement (value engineering)
floors.remove("Basement")
print(floors)
# Output: ["Ground", "First", "Second", "Penthouse"]
# Get floor count
print(f"Building has {len(floors)} floors")
# Output: Building has 4 floors
Common Mistakes
Trying to append multiple items incorrectly
sheets = ["A-101"]
# Wrong - this creates a nested list
sheets.append(["A-102", "A-201"])
print(sheets)
# Output: ["A-101", ["A-102", "A-201"]]
# Correct - use extend
sheets = ["A-101"]
sheets.extend(["A-102", "A-201"])
print(sheets)
# Output: ["A-101", "A-102", "A-201"]
Removing items that don't exist
rooms = ["Office 1", "Office 2"]
rooms.remove("Conference Room")
# Error: ValueError: list.remove(x): x not in list
# Check first
if "Conference Room" in rooms:
rooms.remove("Conference Room")
else:
print("Room not found")
Modifying while iterating (preview — you'll see this more in Module 05)
# Don't do this
rooms = ["Office 1", "Office 2", "Office 3"]
for room in rooms:
if room == "Office 2":
rooms.remove(room) # Modifying while looping causes issues
# We'll cover the correct way to do this in Module 05
For now, just know: modifying a list while looping through it can cause unexpected behavior. We'll address this properly when we cover loops.
Using .pop() without storing the value
sheets = ["A-101", "A-102", "A-201"]
# If you don't need the removed value
sheets.pop() # Works, but wasteful
# Better - use del if you don't need the value
del sheets[-1]
Though honestly, .pop() is fine even if you don't use the return value. This is a minor point.
Assignment
- Create a new file called
modifying_lists.py - Start with an empty list called
sheets - Add these sheets one by one using
.append():- "A-101"
- "A-102"
- "A-201"
- Print the list after adding all three
- Insert "A-150" between "A-102" and "A-201"
- Hint: Find the right index first
- Print the list
- Change "A-201" to "A-202"
- Print the list
- Remove "A-150" using
.remove()- Print the list
- Create a second list with structural sheets:
["S-101", "S-102"]- Add these to your sheets list using
.extend() - Print the combined list
- Add these to your sheets list using
- Remove the last sheet using
.pop()and print what was removed- Print the final list
- Experiment:
- Try to remove a sheet that doesn't exist (see the error)
- Try to use
.pop()on an empty list (see the error)
- Real scenario:
- Start with:
rooms = ["Office 1", "Office 2", "Conference", "Office 3"] - Remove "Conference"
- Insert "Reception" at the beginning
- Change "Office 3" to "Manager Office"
- Add "Break Room" at the end
- Print the final list
- Start with:
Knowledge Check
The following questions are an opportunity to reflect on key topics in this lesson.
- How do you change an item in a list?
- What does .append() do?
- How do you add an item at a specific position?
- What's the difference between .append() and .extend()?
- How do you remove an item by its value?
- What does .pop() return?
- When would you use .remove() vs .pop()?
Additional Resources
This section contains helpful links to related content. It isn't required, so consider it supplemental.
- Python's list methods documentation covers all list methods in detail
- Real Python's list tutorial includes additional examples and edge cases