List Methods
append, insert, remove, pop, sort, reverse — and list comprehensions to build lists in one line.
Yesterday we learned how to read from lists — indexing, slicing, all that. But my customer list isn't static. People sign up, people cancel. How do I add and remove items?
Lists have built-in methods — functions that belong to the list itself. The most important one is .append(), which adds an item to the end:
customers = ["Alice", "Bob"]
customers.append("Charlie")
print(customers) # ["Alice", "Bob", "Charlie"]
Notice that .append() modifies the list in place — it doesn't return a new list. This is a key concept. Most list methods change the original list directly. Think of it like adding a new row at the bottom of your spreadsheet — the spreadsheet itself changes.
What if I need to add someone in the middle? Like, a VIP customer who should go near the top of the list?
Use .insert(index, item). It slides everything after that index to the right:
customers = ["Alice", "Bob", "Charlie"]
customers.insert(1, "VIP_Diana")
print(customers) # ["Alice", "VIP_Diana", "Bob", "Charlie"]
The first argument is where you want to insert, the second is what you're inserting. Everything at that index and beyond shifts right by one position.
And removing? If a customer cancels, I need to take them off the list.
Three ways, depending on what you know:
customers = ["Alice", "Diana", "Bob", "Charlie"]
# Remove by value — removes the first occurrence
customers.remove("Diana")
print(customers) # ["Alice", "Bob", "Charlie"]
# Remove by index — returns the removed item
popped = customers.pop(1)
print(popped) # "Bob"
print(customers) # ["Alice", "Charlie"]
# Remove last item (no index needed)
last = customers.pop()
print(last) # "Charlie"
print(customers) # ["Alice"]
.remove() searches by value — if the value isn't found, it raises a ValueError. .pop() removes by index and gives you the removed item back. Called without an index, .pop() removes the last item. Think of it like pulling a card out of a deck — you get the card, and the deck shrinks.
What about sorting? I have a list of sales figures and I need them highest to lowest for my report.
Two approaches. .sort() sorts the list in place (modifies the original). sorted() returns a new sorted list and leaves the original alone:
sales = [850, 920, 670, 740, 550]
# In-place — changes the original
sales.sort()
print(sales) # [550, 670, 740, 850, 920]
# Reverse sort for your report
sales.sort(reverse=True)
print(sales) # [920, 850, 740, 670, 550]
original = [3, 1, 4, 1, 5]
new_list = sorted(original)
print(original) # [3, 1, 4, 1, 5] — unchanged
print(new_list) # [1, 1, 3, 4, 5] — new sorted list
Use sorted() when you need to keep the original order intact — like when the original order represents the sequence customers signed up.
I've seen these one-liner things for building lists. Like, instead of a loop? They look kind of magical.
List comprehensions — the most Pythonic way to build a list from another sequence. Remember the accumulator pattern from Week 2? Loop, .append(), repeat? Comprehensions do the same thing in one expression:
# Without comprehension (the Week 2 way)
squares = []
for x in range(5):
squares.append(x ** 2)
# squares = [0, 1, 4, 9, 16]
# With comprehension — same result, one line
squares = [x ** 2 for x in range(5)]
This is way better than having square1, square2, square3... You can also add a filter with if:
# Only keep scores above 60
passing = [s for s in scores if s >= 60]
# Get names of high-value customers
vip = [name for name in customers if name.startswith("VIP")]
The pattern is: [expression for item in iterable if condition]. The if part is optional.
When should I use a comprehension vs a regular loop?
Use a comprehension when you're building a new list by transforming or filtering another sequence — that's its sweet spot. Use a regular loop when you're doing something more complex: multiple statements per iteration, nested logic, or when readability would suffer. A good rule: if the comprehension fits on one line and is easy to read, use it. If it starts wrapping or getting confusing, use a loop.
Let's practice these tools.
Practice your skills
Sign up to write and run code in this lesson.
List Methods
append, insert, remove, pop, sort, reverse — and list comprehensions to build lists in one line.
Yesterday we learned how to read from lists — indexing, slicing, all that. But my customer list isn't static. People sign up, people cancel. How do I add and remove items?
Lists have built-in methods — functions that belong to the list itself. The most important one is .append(), which adds an item to the end:
customers = ["Alice", "Bob"]
customers.append("Charlie")
print(customers) # ["Alice", "Bob", "Charlie"]
Notice that .append() modifies the list in place — it doesn't return a new list. This is a key concept. Most list methods change the original list directly. Think of it like adding a new row at the bottom of your spreadsheet — the spreadsheet itself changes.
What if I need to add someone in the middle? Like, a VIP customer who should go near the top of the list?
Use .insert(index, item). It slides everything after that index to the right:
customers = ["Alice", "Bob", "Charlie"]
customers.insert(1, "VIP_Diana")
print(customers) # ["Alice", "VIP_Diana", "Bob", "Charlie"]
The first argument is where you want to insert, the second is what you're inserting. Everything at that index and beyond shifts right by one position.
And removing? If a customer cancels, I need to take them off the list.
Three ways, depending on what you know:
customers = ["Alice", "Diana", "Bob", "Charlie"]
# Remove by value — removes the first occurrence
customers.remove("Diana")
print(customers) # ["Alice", "Bob", "Charlie"]
# Remove by index — returns the removed item
popped = customers.pop(1)
print(popped) # "Bob"
print(customers) # ["Alice", "Charlie"]
# Remove last item (no index needed)
last = customers.pop()
print(last) # "Charlie"
print(customers) # ["Alice"]
.remove() searches by value — if the value isn't found, it raises a ValueError. .pop() removes by index and gives you the removed item back. Called without an index, .pop() removes the last item. Think of it like pulling a card out of a deck — you get the card, and the deck shrinks.
What about sorting? I have a list of sales figures and I need them highest to lowest for my report.
Two approaches. .sort() sorts the list in place (modifies the original). sorted() returns a new sorted list and leaves the original alone:
sales = [850, 920, 670, 740, 550]
# In-place — changes the original
sales.sort()
print(sales) # [550, 670, 740, 850, 920]
# Reverse sort for your report
sales.sort(reverse=True)
print(sales) # [920, 850, 740, 670, 550]
original = [3, 1, 4, 1, 5]
new_list = sorted(original)
print(original) # [3, 1, 4, 1, 5] — unchanged
print(new_list) # [1, 1, 3, 4, 5] — new sorted list
Use sorted() when you need to keep the original order intact — like when the original order represents the sequence customers signed up.
I've seen these one-liner things for building lists. Like, instead of a loop? They look kind of magical.
List comprehensions — the most Pythonic way to build a list from another sequence. Remember the accumulator pattern from Week 2? Loop, .append(), repeat? Comprehensions do the same thing in one expression:
# Without comprehension (the Week 2 way)
squares = []
for x in range(5):
squares.append(x ** 2)
# squares = [0, 1, 4, 9, 16]
# With comprehension — same result, one line
squares = [x ** 2 for x in range(5)]
This is way better than having square1, square2, square3... You can also add a filter with if:
# Only keep scores above 60
passing = [s for s in scores if s >= 60]
# Get names of high-value customers
vip = [name for name in customers if name.startswith("VIP")]
The pattern is: [expression for item in iterable if condition]. The if part is optional.
When should I use a comprehension vs a regular loop?
Use a comprehension when you're building a new list by transforming or filtering another sequence — that's its sweet spot. Use a regular loop when you're doing something more complex: multiple statements per iteration, nested logic, or when readability would suffer. A good rule: if the comprehension fits on one line and is easy to read, use it. If it starts wrapping or getting confusing, use a loop.
Let's practice these tools.
Practice your skills
Sign up to write and run code in this lesson.