fruits = ["apple", "banana"]
fruits[0] = "mango" # change an item
fruits.append("cherry") # add to the end
fruits.insert(1, "kiwi") # insert at position 1
print(fruits)
# ['mango', 'kiwi', 'banana', 'cherry']
๐๏ธ Removing items
fruits = ["apple", "banana", "cherry"]
fruits.remove("banana") # remove by value
last = fruits.pop() # remove & return the last item
print(fruits) # ['apple']
print(last) # cherry
๐ Looping through a list
This is where lists shine โ a for loop visits every item:
scores = [90, 85, 100]
for s in scores:
print(f"Score: {s}")
๐ ๏ธ Handy list tools
nums = [3, 1, 4, 1, 5]
print(len(nums)) # 5 how many items
print(sum(nums)) # 14 add them all
print(max(nums)) # 5 biggest
print(min(nums)) # 1 smallest
nums.sort() # put in order
print(nums) # [1, 1, 3, 4, 5]
print("apple" in fruits) # True/False โ is it there?
โ What you learned
Lists hold ordered collections in [ ].
Read items by index (starting at 0); [-1] is the last.
Add with .append() / .insert(), remove with .remove() / .pop().
Loop with for; use len, sum, max, min, sort, and in.
๐ฎ Time to Practice!
Collect, change, and explore lists of stuff. ๐ฆ
๐
Task 1: Print Each Color
Easy
A list colors holds "red", "green", "blue". Use a for loop to print each color on its own line.
colors = ["red", "green", "blue"]
for c in colors:
print(c)
๐
Task 2: Shopping List
Medium
Start with cart = ["milk", "eggs"]. Add "bread" to the end, then print the whole list. Expected: ['milk', 'eggs', 'bread']
Make a list of your 3 favorite songs. Add one more, remove one, sort it, and print it after each change to watch it transform.
๐๏ธ Extra Practice โ Drill Time!
Real programs are full of lists โ scores, names, messages, pixels. Do all of these and lists will stop feeling scary.
๐
Task 6: Pick and Slice
Easy
With animals = ["cat", "dog", "fox", "owl", "bee"], print: the first animal, the last animal (use [-1]), the first three as a list ([0:3]) and how many animals there are.
Start with bag = ["apple", "pen"]. Append "book", insert "key" at position 0, remove "pen", then print the bag. Finally use .pop() to take out the last item, print the popped item, and print the bag again.
['key', 'apple', 'book']
book
['key', 'apple']
bag = ["apple", "pen"]
bag.append("book")
bag.insert(0, "key")
bag.remove("pen")
print(bag)
last = bag.pop()
print(last)
print(bag)
๐
Task 8: Sort It Out
Medium
With nums = [5, 2, 9, 1]: print sorted(nums) (a sorted copy), then print nums to show the original is untouched. Then call nums.sort(reverse=True) and print nums โ now it really changed.
With votes = ["red", "blue", "red", "green", "red"], print: whether "blue" is in the list, how many votes "red" got (.count()), and the position of the first "green" (.index()).
Print a numbered menu from menu = ["pizza", "salad", "soup"] so it reads 1. pizza, 2. salad, 3. soup. Use for i, item in enumerate(menu, start=1):.
1. pizza
2. salad
3. soup
menu = ["pizza", "salad", "soup"]
for i, item in enumerate(menu, start=1):
print(f"{i}. {item}")
๐งน
Task 11: Build a New List
Medium
From numbers = [4, 7, 10, 3, 8, 15], build a brand-new list called evens containing only the even numbers, then print it. Start with evens = [] and append inside a loop.
[4, 10, 8]
numbers = [4, 7, 10, 3, 8, 15]
evens = []
for n in numbers:
if n % 2 == 0:
evens.append(n)
print(evens)
๐๏ธ
Task 12: One-Line Magic
Tricky
A list comprehension does the last task in one line: [n * n for n in numbers]. From numbers = [1, 2, 3, 4, 5], print the squares in one line, then print only the odd numbers using [n for n in numbers if n % 2 == 1].
[1, 4, 9, 16, 25]
[1, 3, 5]
numbers = [1, 2, 3, 4, 5]
print([n * n for n in numbers])
print([n for n in numbers if n % 2 == 1])
๐บ๏ธ
Task 13: A List of Lists
Tricky
A grid is a list of rows: grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]. Print the middle number (grid[1][1]), then print each row on its own line with the numbers separated by spaces.
5
1 2 3
4 5 6
7 8 9
grid = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
print(grid[1][1])
for row in grid:
print(row[0], row[1], row[2])
๐
Task 14: Boss Level: Leaderboard
Boss
You have players = ["Ana", "Ben", "Cy", "Dee"] and scores = [42, 91, 67, 88]. Print the top three, highest first, as 1. Ben - 91 and so on. Hint: list(zip(scores, players)) then sorted(..., reverse=True).
1. Ben - 91
2. Dee - 88
3. Cy - 67
players = ["Ana", "Ben", "Cy", "Dee"]
scores = [42, 91, 67, 88]
board = sorted(zip(scores, players), reverse=True)
for i, pair in enumerate(board[:3], start=1):
print(f"{i}. {pair[1]} - {pair[0]}")
๐ง
Task 15: Free Play โ Your Top 10
Creative
Make a list of your ten favorite songs, games or foods. Print it numbered, print it sorted alphabetically, print how many items it has, then add one and remove one. ๐ถ