WasiLearn Code
Python · Getting started

Lists: many values, one name

A variable holds one thing. A list holds many, in order, under a single name.

Making a list

Square brackets, values separated by commas:

scores = [10, 8, 9, 7]
names = ["Ayesha", "Bilal", "Chen"]

print(scores)
print(names)
print(len(names))
output
[10, 8, 9, 7]
['Ayesha', 'Bilal', 'Chen']
3

len() — the same function you met with strings — gives the number of items.

Getting one item out

Square brackets again, with a position:

names = ["Ayesha", "Bilal", "Chen"]

print(names[0])
print(names[1])
print(names[2])
output
Ayesha
Bilal
Chen

Positions start at zero. The first item is names[0], not names[1].

This feels wrong to everyone at first and is the single most common source of list bugs. A useful way to think about it: the number is not "which item" but "how far along from the start". The first item is zero steps from the start.

So the last item of a three-item list is at position 2, not 3:

names = ["Ayesha", "Bilal", "Chen"]
print(names[2])
print(names[-1])
output
Chen
Chen

-1 means "the last one" — much safer than counting, because it stays correct when the list changes size.

Ask for a position that does not exist and Python stops with an error:

names = ["Ayesha", "Bilal", "Chen"]
print(names[3])

Read the message: list index out of range. It means exactly what it says, and it is almost always an off-by-one.

Changing a list

Unlike strings, lists can be changed in place:

scores = [10, 8, 9]

scores[1] = 100        # replace an item
scores.append(7)       # add to the end
scores.remove(10)      # remove by value

print(scores)
output
[100, 9, 7]

Compare this with the strings lesson, where name.upper() gave back a new string and left the original alone. scores.append(7) changes scores itself and returns nothing. That difference matters:

scores = [1, 2]
result = scores.append(3)

print(scores)
print(result)
output
[1, 2, 3]
None

None is Python's word for "no value at all". If you ever print a list and get None, you have almost certainly written scores = scores.append(3) — storing the nothing that append handed back, and destroying your list in the process.

Looping over a list

This is where lists and loops meet, and it is the most useful thing in this lesson:

names = ["Ayesha", "Bilal", "Chen"]

for name in names:
    print(f"Hello, {name}!")
output
Hello, Ayesha!
Hello, Bilal!
Hello, Chen!

No range(), no positions, no counting. Python walks the list and hands you one item at a time. This is almost always what you want.

Totals and averages

The building-up pattern from the loops lesson, now on real data:

scores = [10, 8, 9, 7, 6]
total = 0

for score in scores:
    total += score

print(f"Total: {total}")
print(f"Average: {total / len(scores)}")
output
Total: 40
Average: 8.0

Python can do the total for you, and for anything real you should let it:

scores = [10, 8, 9, 7, 6]

print(sum(scores))
print(max(scores))
print(min(scores))
print(sorted(scores))
output
40
10
6
[6, 7, 8, 9, 10]

Writing the loop yourself first is worth doing once, so you know what sum() is actually doing. After that, use sum().

Checking whether something is in a list

names = ["Ayesha", "Bilal", "Chen"]

if "Bilal" in names:
    print("Bilal is here")

if "Dana" not in names:
    print("Dana is not")
output
Bilal is here
Dana is not

in reads exactly like English and saves you writing a loop to search.

Prefer learning with a teacher?

WasiLearn Academy runs small-group classes covering this material, online and in Karachi.

See the classes