Skip to content
BetaDE
Lesson 5 of 35Python

Python · Lesson 5 · 25 minutes

Lists – many values at once

What you'll learn

  • Create lists and access items by position (index)
  • Append, change and count items
  • Analyse lists with sum(), max() and a loop

A box with compartments

So far every variable held exactly one value. A list stores many values in a fixed order. You write it in square brackets, separated by commas:

friends = ["Mia", "Ben", "Lea", "Tom"]
print(friends)
print(len(friends))

len() counts how many items are in the list.

Accessing single items

Every item has a position, the index. Careful: Python starts counting at 0!

Index0123
Value“Mia”“Ben”“Lea”“Tom”
friends = ["Mia", "Ben", "Lea", "Tom"]
print(friends[0])
print(friends[2])
print(friends[-1])

With [-1] you get the last item – handy when you don’t know how long the list is.

Changing lists

Lists can be changed: you can append, replace and remove items. Press Steps and watch the list in memory:

backpack = ["bottle", "bread"]
backpack.append("apple")
backpack[0] = "water bottle"
backpack.remove("bread")
print(backpack)
print("apple" in backpack)

With in you check whether something is in the list. The result is True or False.

Analysing lists

For lists of numbers Python has ready-made tools: sum() adds everything up, max() and min() find the biggest and smallest value. And with for you go through every item one by one:

temperatures = [18, 21, 25, 19, 23]

print(f"Warmest day: {max(temperatures)} degrees")
print(f"Total: {sum(temperatures)}")

for t in temperatures:
    if t >= 20:
        print(f"{t} degrees – T-shirt weather")

Remember: The first index is 0, the last is len(list) - 1. If you write list[len(list)], you get an IndexError.

Quick check

Before you start the task: what does this program print? Type your guess – then Python works it out.

animals = ["dog", "cat", "mouse"]
animals.append("hedgehog")
print(animals[1], len(animals))

Your task

Your turn

The list points contains the results of a game. Print three lines: Highest score: 19, Average: 11.2 (rounded to one decimal place with round(value, 1)) and Over 10 points: 3 (how many values are greater than 10). Let Python work everything out – don't type the numbers yourself, because the test also tries a different list.

points = [12, 7, 19, 3, 15]

# Analyse the list here:

Solve it without hints to earn a ⭐ on your progress page.

Show solution

Try it yourself first – mistakes teach you the most. Here is one possible solution:

points = [12, 7, 19, 3, 15]

print(f"Highest score: {max(points)}")
print(f"Average: {round(sum(points) / len(points), 1)}")

count = 0
for p in points:
    if p > 10:
        count = count + 1
print(f"Over 10 points: {count}")

For fast learners

Pro task

Find the second largest number in numbers – without sorted(), .sort() and without max(). Equal numbers only count once: in [4, 9, 2, 9, 7] the second largest is 7. Print Second largest: 7.

numbers = [4, 9, 2, 9, 7]

# Remember the largest and the second largest number while going through
Show pro solution
numbers = [4, 9, 2, 9, 7]

largest = None
second = None
for n in numbers:
    if largest is None or n > largest:
        second = largest
        largest = n
    elif n != largest and (second is None or n > second):
        second = n
print(f"Second largest: {second}")
Practise more in the training roomEndless new tasks on this topic – with XP and streaks.