Session 7 · Lists: the delivery man’s busy day#

📖 Based on the blog posts Python List: The Delivery Man’s Busy Day and Task: The Delivery Man.

The big idea#

A list is an ordered row of items that you can change: add, remove, sort, reorder. It’s Python’s most used collection.

Everyday example: meet Alex, a delivery man. His truck has a row of bins, and each bin holds a package. Let’s follow his day.

Alex's truck: list methods A list is an ordered, changeable row of items. append adds at the end, insert at a position, remove deletes by value, pop removes and returns the last item. Letter [0] Fragile Box [1] Parcel [2] Special Delivery [3] append() ⟶ new item at the end insert(1, x) put x at index 1 remove("Box") delete first "Box" pop() remove + return last sort() / reverse() reorder in place
A list is an ordered, changeable row of items. append adds at the end, insert at a position, remove deletes by value, pop removes and returns the last item.

Morning: loading the truck#

packages = ["Letter", "Box", "Parcel"]
print(packages, len(packages))
['Letter', 'Box', 'Parcel'] 3

Square brackets, items separated by commas. A list can hold anything, even mixed types: [1, "two", 3.0, True].

Through the day#

packages = ["Letter", "Box", "Parcel"]

item = packages[1]                         # a customer needs the Box (index 1)
print("Deliver:", item)

packages.append("Special Delivery")        # new package at the end
packages.insert(2, "Fragile Box")          # squeeze one in at index 2
print(packages)

packages.remove("Box")                     # loaded by mistake: remove by value
print(packages)

last_package = packages.pop()              # take the last one off, and get it back
print("Took off:", last_package)
print(packages)

print("Parcel is in bin", packages.index("Parcel"))
print(packages[0:3])                       # a slice, like Session 6
Deliver: Box
['Letter', 'Box', 'Fragile Box', 'Parcel', 'Special Delivery']
['Letter', 'Fragile Box', 'Parcel', 'Special Delivery']
Took off: Special Delivery
['Letter', 'Fragile Box', 'Parcel']
Parcel is in bin 2
['Letter', 'Fragile Box', 'Parcel']

Evening: organising#

packages = ["Letter", "Fragile Box", "Parcel"]
packages.sort()            # alphabetical, changes the list in place
print(packages)
packages.reverse()         # reverse order, in place
print(packages)
['Fragile Box', 'Letter', 'Parcel']
['Parcel', 'Letter', 'Fragile Box']

Warning

sort() and reverse() change the list and return None. packages = packages.sort() loses your list! Use sorted(packages) when you want a new sorted list and to keep the original.

Cheat sheet#

What

How

add at the end / at a position

append(x) / insert(i, x)

add many

extend(other_list) or +

remove by value / by position

remove(x) / pop(i) or del lst[i]

find / count

index(x) / count(x)

is it there?

x in lst

change an item

lst[i] = new

length

len(lst)

sort / reverse

sort() / reverse() (in place) or sorted() / lst[::-1] (new list)

empty it

clear()

loop

for item in lst:

List comprehensions#

A short way to build a new list from another one:

items = ["Notebook", "Pencil", "Eraser"]
upper = [item.upper() for item in items]
short = [item for item in items if len(item) <= 6]
print(upper)
print(short)
['NOTEBOOK', 'PENCIL', 'ERASER']
['Pencil', 'Eraser']

Read it as “item.upper() for each item in items”, optionally “if …”.

Common mistakes#

  • lst = lst.sort(): makes lst None (see the warning above).

  • remove() a missing item: ValueError. Check if x in lst: first.

  • Changing a list while looping over it: items get skipped. Loop over a copy (for x in lst[:]) or build a new list with a comprehension.

  • b = a to copy: both names share one list (Session 3).

Hands-on exercises#

The 19 tasks from Task: The Delivery Man. Each solution starts from the same list so you can run it on its own.

Task 1. Create a list of five delivery items and print the third.

Solution
items = ["Notebook", "Pencil", "Eraser", "Ruler", "Marker"]
print(items[2])        # Eraser

Task 2. Add “Glue Stick” to the end and print the list.

Solution
items = ["Notebook", "Pencil", "Eraser", "Ruler", "Marker"]
items.append("Glue Stick")
print(items)

Task 3. Insert “Highlighter” between the second and third items.

Solution
items = ["Notebook", "Pencil", "Eraser", "Ruler", "Marker"]
items.insert(2, "Highlighter")
print(items)    # ['Notebook', 'Pencil', 'Highlighter', 'Eraser', 'Ruler', 'Marker']

Task 4. A delivery was cancelled: remove “Ruler”.

Solution
items = ["Notebook", "Pencil", "Eraser", "Ruler", "Marker"]
items.remove("Ruler")
print(items)

Task 5. Print only the first three items.

Solution
items = ["Notebook", "Pencil", "Eraser", "Ruler", "Marker"]
print(items[:3])

Task 6. Make an uppercase copy with a list comprehension.

Solution
items = ["Notebook", "Pencil", "Eraser", "Ruler", "Marker"]
print([item.upper() for item in items])

Task 7. Check whether “Marker” is in the list and print a message.

Solution
items = ["Notebook", "Pencil", "Eraser", "Ruler", "Marker"]
if "Marker" in items:
    print("Marker is still to be delivered")
else:
    print("Marker is not in the list")

Task 8. Print the number of items.

Solution
items = ["Notebook", "Pencil", "Eraser", "Ruler", "Marker"]
print(len(items))      # 5

Task 9. Sort the list alphabetically.

Solution
items = ["Notebook", "Pencil", "Eraser", "Ruler", "Marker"]
items.sort()
print(items)    # ['Eraser', 'Marker', 'Notebook', 'Pencil', 'Ruler']

Task 10. Reverse the order of deliveries.

Solution
items = ["Notebook", "Pencil", "Eraser", "Ruler", "Marker"]
items.reverse()
print(items)

Task 11. Make a list where each element is [item, delivery_time]. Print the first item and its time.

Solution
schedule = [["Notebook", "09:00"], ["Pencil", "10:30"], ["Eraser", "12:15"]]
item, time = schedule[0]
print(f"{item} at {time}")      # Notebook at 09:00

Task 12. Count how many times “Ruler” appears.

Solution
items = ["Notebook", "Ruler", "Pencil", "Ruler"]
print(items.count("Ruler"))     # 2

Task 13. Find the index of “Pencil”.

Solution
items = ["Notebook", "Pencil", "Eraser", "Ruler", "Marker"]
print(items.index("Pencil"))    # 1

Task 14. Extend the list with another list of new items.

Solution
items = ["Notebook", "Pencil"]
items.extend(["Stapler", "Sharpener"])
print(items)

append(["Stapler", "Sharpener"]) would add one item: a list inside the list.

Task 15. Clear the list.

Solution
items = ["Notebook", "Pencil", "Eraser", "Ruler", "Marker"]
items.clear()
print(items)    # []

Task 16. Make a list with “Notebook” repeated three times.

Solution
print(["Notebook"] * 3)

Task 17. With a comprehension, build [item, length] pairs.

Solution
items = ["Notebook", "Pencil", "Eraser", "Ruler", "Marker"]
print([[item, len(item)] for item in items])

Task 18. Keep only items that contain the letter “e”.

Solution
items = ["Notebook", "Pencil", "Eraser", "Ruler", "Marker", "Glue Stick", "Map"]
print([item for item in items if "e" in item])

The first five all contain a lowercase “e” (check Eraser: E-r-a-s-e-r); “Map” is filtered out. "e" in item is case-sensitive.

Task 19. Remove duplicates.

Solution
items = ["Pencil", "Ruler", "Pencil", "Marker", "Ruler"]
unique = list(dict.fromkeys(items))     # keeps the original order
print(unique)                           # ['Pencil', 'Ruler', 'Marker']

list(set(items)) also removes duplicates, but loses the order (Session 10).