Skip to content
Back to course

6. Lists and collections

So far, every variable you have used holds one value at a time — a name, a number, or a True/False. But real programs need to work with groups of values: the prices of items in Abebe's shop, the names of students in a class, or the daily TeleBirr transaction amounts for a week. Python's list is the simplest and most used way to store many values in a single variable. In this lesson you will learn to create lists, read values from them, change them, and loop over them to process every item.

A list is created with square brackets [ ] and items separated by commas. A list can hold numbers, strings, booleans — even a mix of different types.

Creating lists:

# A list of shop items at Almaz's stall in Merkato

items = ["teff", "sugar", "oil", "salt"]

# A list of prices in birr

prices = [45, 120, 85, 30]

# A mixed list

info = ["Sara", 28, True]

# An empty list (you will add to it later)

cart = []

To see a whole list, use print():

print(items) # ['teff', 'sugar', 'oil', 'salt']

print(prices) # [45, 120, 85, 30]

To see how many items are in a list, use len():

print(len(items)) # 4

print(len(cart)) # 0

Every item in a list has a position number called an index. Python indexes start at 0, not 1.

fruits = ["mango", "banana", "papaya", "avocado"]

# index: 0 1 2 3

Accessing items:

print(fruits[0]) # mango (first item)

print(fruits[1]) # banana (second item)

print(fruits[3]) # avocado (fourth item)

Negative indexes count from the end:

print(fruits[-1]) # avocado (last item)

print(fruits[-2]) # papaya (second-to-last)

Changing an item by index:

fruits[1] = "orange" # replace banana with orange

print(fruits) # ['mango', 'orange', 'papaya', 'avocado']

Trying to use an index that does not exist causes an IndexError:

print(fruits[10]) # ERROR: list index out of range

Remember: Python always starts counting at 0. So the first item is at index 0, the second at index 1, and so on. The last item is always at index len(list) - 1. This trips up many beginners — if your list has 4 items, the valid indexes are 0, 1, 2, and 3. Index 4 does not exist!

Lists are mutable — you can add, insert, or remove items after they are created. Python gives you built-in methods for this.

Adding to the end with append():

cart = []

cart.append("teff")

cart.append("sugar")

print(cart) # ['teff', 'sugar']

Inserting at a specific position with insert(index, value):

cart.insert(1, "oil") # insert 'oil' at position 1

print(cart) # ['teff', 'oil', 'sugar']

Removing by value with remove() — removes the first matching item:

cart.remove("oil")

print(cart) # ['teff', 'sugar']

Removing by position with pop() — removes and returns the item at that index:

last = cart.pop() # removes and returns the last item

print(last) # sugar

print(cart) # ['teff']

cart.pop(0) # remove item at index 0

print(cart) # []

The most common thing you do with a list is loop over it to process every item. You already know the for loop from the previous lesson — it works perfectly with lists.

Example — print a price list for Abebe's shop:

items = ["teff", "sugar", "oil", "salt"]

prices = [45, 120, 85, 30 ]

for i in range(len(items)):

print(items[i], "—", prices[i], "birr")

Output:

teff — 45 birr

sugar — 120 birr

oil — 85 birr

salt — 30 birr

If you only need the values (not the index), Python's for-in-list form is even simpler:

for item in items:

print(item)

Example — add up all prices to get the total basket cost:

total = 0

for price in prices:

total = total + price

print("Total:", total, "birr") # Total: 280 birr

Putting it together — Sara tracks her TeleBirr transactions for the week: transactions = [] transactions.append(150) # Monday: received 150 birr transactions.append(320) # Tuesday transactions.append(80) # Wednesday transactions.append(500) # Thursday transactions.append(210) # Friday total = 0 for amount in transactions: total = total + amount print("Number of transactions:", len(transactions)) # 5 print("Total received:", total, "birr") # 1260 birr print("Largest single amount:", max(transactions)) # 500 print("Smallest single amount:", min(transactions)) # 80 max() and min() are built-in Python functions that find the largest and smallest values in a list.

Scenario

Almaz runs a small grocery in Addis Ababa. She stores item names in a list: stock = ["teff", "oil", "sugar", "salt", "flour"] She wants to add "coffee" to the end, then remove "oil" because it is out of stock, then print the total number of remaining items. Which three lines of code should she write (in order)?

Lesson recap: • A list [ ] stores many values in one variable. Items can be any type. • len(list) returns the number of items. • Indexes start at 0 — first item is list[0], last is list[-1]. • You can read, change, add, and remove items after creating the list. • append() adds to the end; insert(i, val) adds at position i. • remove(val) removes the first match; pop(i) removes and returns item at position i (default: last). • Loop over a list with for item in my_list: to process every value. • sort() orders items in place; max() and min() find the largest/smallest values.

Check your understanding

1/7 · 80 XP

What is the correct way to create a list of three student names in Python?