6. Lists and collections
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
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
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)?
Check your understanding
1/7 · 80 XPWhat is the correct way to create a list of three student names in Python?