Lists in Depth

Python 3.12 · ✓ verified by execution on 2026-07-22

Lists in Python are dynamic, meaning they can grow, shrink, and be rearranged. Let’s explore the powerful built-in methods that let you manipulate lists.

Adding Items

You can add items to a list using append() or insert(). append() places the new item at the very end. insert() lets you choose the exact position.

python
fruits = ['apple', 'banana']
fruits.append('cherry')
print(fruits)
Output
['apple', 'banana', 'cherry']
python
fruits = ['apple', 'cherry']
fruits.insert(1, 'banana')
print(fruits)
Output
['apple', 'banana', 'cherry']

Edge Cases

Removing Items

You can remove items by value using remove() or by position using pop().

python
fruits = ['apple', 'banana', 'apple']
fruits.remove('apple')
print(fruits)
Output
['banana', 'apple']

Notice that remove() only deleted the first occurrence of 'apple'.

python
fruits = ['apple', 'banana', 'cherry']
last = fruits.pop()
print(last)
print(fruits)
Output
cherry
['apple', 'banana']

If you don’t provide a position, pop() assumes you want the last item.

Edge Cases

Sorting and In-Place Modifications

Lists can be sorted directly using the sort() method.

python
numbers = [3, 1, 4, 1, 5, 9]
numbers.sort()
print(numbers)
Output
[1, 1, 3, 4, 5, 9]

Predict the Output: In-Place vs Return

Unlike string methods (which return brand new strings), list methods like sort() modify the original list in place. Because they alter the existing object, they don’t need to return a new list. In fact, they return None.

Predict what happens if you try to assign the result of sort() to a variable!

python
numbers = [3, 1, 2]
result = numbers.sort()
print(result)
Output
None

If you ever see None when you expected a list, you probably assigned the result of an in-place method!

Check yourself

Does the sort() method return a new sorted list?

Reveal answer

No, sort() modifies the list in place and returns None. — List methods like sort() modify the list in place and evaluate to None.

If a list has multiple identical items, what does remove() delete?

Reveal answer

Only the very first occurrence of the specified value. — remove() scans from the beginning and stops after deleting the first match.

What happens if you call pop() without providing an index?

Reveal answer

It removes and returns the very last item in the list. — By default, pop() targets the last item (index -1).

Challenges

Challenge 1 +15 XP

You are managing a line at a store. Add 'Charlie' to the end of the line, then serve the first person by removing them from the front of the line (index 0). Print the final line.

python
  • Test 1 — expects "['Bob', 'Charlie']\n"
Need a hint? (−25% XP)

Use append('Charlie') to add to the end, and pop(0) to remove the first person.

Show solution (0 XP)
line = ['Alice', 'Bob']
line.append('Charlie')
line.pop(0)
print(line)

Challenge 2 +20 XP

Sort the list of high scores from lowest to highest, then remove the lowest score. Print the resulting list.

python
  • Test 1 — expects "[82, 88, 95, 100]\n"
Need a hint? (−25% XP)

First call sort() on the list. Since it sorts lowest-to-highest by default, the lowest score will be at index 0. Then call pop(0).

Show solution (0 XP)
scores = [95, 82, 100, 75, 88]
scores.sort()
scores.pop(0)
print(scores)