Learning Goals
3 minBy the end of this lesson you can:
- Use the dot-method syntax —
my_list.append(x)— to call a method on a list. - Grow, shrink, and reorder a list with
append,pop,remove, andsort. - Explain why
removeonly takes out the first match and whypophands the removed item back to you.
Warm-Up
5 minLast lesson we walked every item in a list with for item in list. Useful — but the lists themselves never changed. In real apps lists grow when users add things and shrink when users delete things. Today we learn the tools that change a list.
Predict the output. What does Python print?
queue = ["Hafiz", "Aisyah", "Daniel"] queue.append("Mei") print(queue) print(len(queue))
Show the answer
['Hafiz', 'Aisyah', 'Daniel', 'Mei'] 4
queue.append("Mei") sticks "Mei" on the end. The list now has four names and len(queue) bumps from 3 to 4. Notice we did not write queue = queue.append("Mei") — methods like append change the list in place and don't need re-assigning.
New Concept · The Five Tools
14 minDot syntax · asking a list to do something
Until now we've called functions like print(thing) and len(thing). Today we meet a different shape: thing.action(). The dot reads as "ask thing to action itself."
names = ["Hafiz", "Aisyah"] names.append("Daniel") # ask names to append "Daniel" to itself print(names) # → ['Hafiz', 'Aisyah', 'Daniel']
These dotted actions are called methods. A list has many of them; today we meet the four most useful.
1 · append(x) — add to the end
orders = [] orders.append("teh tarik") orders.append("roti canai") orders.append("nasi lemak") print(orders) # → ['teh tarik', 'roti canai', 'nasi lemak']
Starting from an empty list [] and appending is the most common pattern in Python. Think of append as "and one more, please."
2 · pop() — take off the end
pop does two things in one move: it removes the last item and hands that item back to you.
orders = ["teh tarik", "roti canai", "nasi lemak"] served = orders.pop() print("Served:", served) print("Still waiting:", orders) # → Served: nasi lemak # → Still waiting: ['teh tarik', 'roti canai']
You can also pop a specific position — orders.pop(0) takes off the first item (a fair queue). Without a number, pop() means "the last one."
3 · remove(x) — find and delete by value
When you don't know the position but you know the value, use remove.
queue = ["Hafiz", "Aisyah", "Daniel", "Mei"] queue.remove("Daniel") print(queue) # → ['Hafiz', 'Aisyah', 'Mei']
Gotcha. remove only deletes the first match. If "Daniel" appears twice, the second one stays put. And if the value isn't there at all, Python crashes with ValueError: list.remove(x): x not in list.
numbers = [1, 2, 3, 2, 1] numbers.remove(2) print(numbers) # → [1, 3, 2, 1] ← only the first 2 is gone
4 · sort() — tidy in order
sort rearranges the list so values go from smallest to largest (numbers) or A to Z (strings).
prices = [5.50, 3.00, 8.00, 2.50] prices.sort() print(prices) # → [2.5, 3.0, 5.5, 8.0] names = ["Mei", "Hafiz", "Aisyah", "Daniel"] names.sort() print(names) # → ['Aisyah', 'Daniel', 'Hafiz', 'Mei']
Want biggest-first or Z-to-A? Pass reverse=True.
prices = [5.50, 3.00, 8.00, 2.50] prices.sort(reverse=True) print(prices) # → [8.0, 5.5, 3.0, 2.5]
5 · len() — the size, revisited
You met len() two lessons ago. It is the odd one out today because it's a regular function, not a method — len(my_list), not my_list.len(). It pairs beautifully with the others: append raises the length, pop and remove lower it.
These methods change the list in place. They return None (or, for pop, the removed item). So queue = queue.append("Mei") would set queue to None and wipe out your list. Just write queue.append("Mei") on its own line.
Worked Example · Aisyah's Tuition Queue
12 minThe job
Aisyah runs a small tuition class in KL. Students sign in as they arrive — first come, first served. We'll build a tiny queue using every method from this lesson.
Step 1 · Start with an empty queue
queue = []
An empty list [] is our blank sign-up sheet.
Step 2 · Three students arrive
queue.append("Hafiz") queue.append("Aisyah") queue.append("Daniel") print("Queue:", queue) # → Queue: ['Hafiz', 'Aisyah', 'Daniel']
Step 3 · Call the next student
First-come-first-served means we pop from the front — index 0.
next_up = queue.pop(0) print("Now serving:", next_up) print("Queue:", queue) # → Now serving: Hafiz # → Queue: ['Aisyah', 'Daniel']
Step 4 · A latecomer arrives
queue.append("Mei") print("Queue:", queue) # → Queue: ['Aisyah', 'Daniel', 'Mei']
Step 5 · Daniel cancels
He calls Aisyah and says he can't make it. We know the name but not the position — perfect for remove.
queue.remove("Daniel") print("Queue:", queue) # → Queue: ['Aisyah', 'Mei']
Step 6 · End of session · tidy the register
queue.sort() print("Final register:", queue) print("How many today:", len(queue)) # → Final register: ['Aisyah', 'Mei'] # → How many today: 2
The complete script
Save as tuition_queue.py:
Code
# tuition_queue.py — Aisyah's tuition sign-in queue = [] queue.append("Hafiz") queue.append("Aisyah") queue.append("Daniel") print("Queue:", queue) next_up = queue.pop(0) print("Now serving:", next_up) print("Queue:", queue) queue.append("Mei") print("Queue:", queue) queue.remove("Daniel") print("Queue:", queue) queue.sort() print("Final register:", queue) print("How many today:", len(queue))
Output
Queue: ['Hafiz', 'Aisyah', 'Daniel'] Now serving: Hafiz Queue: ['Aisyah', 'Daniel'] Queue: ['Aisyah', 'Daniel', 'Mei'] Queue: ['Aisyah', 'Mei'] Final register: ['Aisyah', 'Mei'] How many today: 2
Sign-up sheets, shopping carts, music playlists, undo buffers — they're all just lists that grow with append, shrink with pop or remove, and occasionally get tidied with sort. Same five tools, infinite uses.
Try It Yourself
13 minThree tasks. Type, run, only peek at the hint if you're truly stuck.
Start with an empty list tray = []. Append four kuih names of your choice (e.g. "kuih lapis", "ondeh-ondeh", "seri muka", "kuih bahulu"). Print the tray and the count.
Hint
tray = [] tray.append("kuih lapis") tray.append("ondeh-ondeh") tray.append("seri muka") tray.append("kuih bahulu") print(tray) print("How many:", len(tray))
Each append goes on its own line. Don't write tray = tray.append(...) — that would wipe the tray to None.
Start with prices = [4.50, 2.00, 6.00, 3.50, 5.00]. Two things sell out — pop() the last price and print which one was sold. Then remove(3.50). Finally sort() the leftovers and print them.
Hint
prices = [4.50, 2.00, 6.00, 3.50, 5.00] sold = prices.pop() print("Sold for RM", sold) prices.remove(3.50) prices.sort() print("Leftover prices:", prices)
pop() with no argument takes the last item and hands it back. remove(3.50) deletes the first match by value. sort() rearranges in place — no need to re-assign.
Given scores = [62, 88, 41, 95, 73, 58, 90], print the top three scores in descending order. Use sort(reverse=True) plus what you already know about indexing.
Hint
scores = [62, 88, 41, 95, 73, 58, 90] scores.sort(reverse=True) print("Top 3:", scores[0], scores[1], scores[2])
Sort biggest-first, then read positions 0, 1, 2. Next lesson we'll do this in one clean step with slicing — scores[:3] — but indexing works fine for now.
Mini-Challenge · Hafiz's Disappearing Friend
8 minHafiz is building a guest-list app. Two people on his list happen to share the name "Daniel" (a Daniel Tan and a Daniel Lim). One Daniel cancels, so Hafiz writes:
guests = ["Aisyah", "Daniel", "Mei", "Daniel", "Priya"] guests.remove("Daniel") guests.remove("Daniel") guests.remove("Daniel") print(guests)
The program crashes. Why? And what would you change so it removes both Daniels without crashing?
Show the fix
guests = ["Aisyah", "Daniel", "Mei", "Daniel", "Priya"] guests.remove("Daniel") guests.remove("Daniel") print(guests) # → ['Aisyah', 'Mei', 'Priya']
The third remove("Daniel") blew up with ValueError because there were only two Daniels — the third call had nothing to remove. Always match the number of remove calls to the number of items you actually have. (A nicer pattern with while "Daniel" in guests is coming in Level 2 — for now, count carefully.)
Recap
3 minLists are no longer frozen. append grows them, pop and remove shrink them, sort reorders them. All four are methods — you call them with a dot — and they change the list in place, so never re-assign the result.
Vocabulary Card
- method
- A function attached to a value. Called with dot syntax —
my_list.append(x). append(x)- Adds
xto the end of the list. The list grows by one. pop()/pop(i)- Removes and returns the last item (or the item at index
i). remove(x)- Deletes the first occurrence of
x. Crashes ifxisn't in the list. sort()- Reorders the list in place, smallest to largest. Add
reverse=Truefor largest first. - in place
- The method changes the original list. It does not hand back a new list — so don't write
x = x.sort().
Homework
4 minCreate a new file playlist.py — a tiny music playlist manager.
- Start with
playlist = []. appendfour song titles of your choice.- Use
pop(0)to "play" the next song — printNow playing: <song>. appendtwo more songs (someone added requests).removeone song you no longer want.sort()the playlist so the remaining songs are in A–Z order.- Print the final playlist and its length.
Bring playlist.py next class — in PY-L1-18 we'll slice lists to grab the first three or last three songs in one move.
Sample · playlist.py
# playlist.py — a tiny music playlist manager playlist = [] playlist.append("Sayang Kinabalu") playlist.append("Cinta Sampai Mati") playlist.append("Bunga") playlist.append("Rasa Sayang") now_playing = playlist.pop(0) print("Now playing:", now_playing) playlist.append("Negaraku") playlist.append("Standing in the Eyes of the World") playlist.remove("Bunga") playlist.sort() print("Playlist:", playlist) print("Songs left:", len(playlist))
Your songs can be different — the methods are what matter. The non-negotiables are: starting from an empty list, calling each method on its own line without re-assigning, and using pop(0) (not just pop()) so the first song plays. If you used pop(), you played the last song — that still works for the structure, but isn't how a real playlist queue behaves.