Many values, one name
A teacher in Hanoi records the marks of 32 students. With what you know so far, she would need 32 variables, mark1, mark2 and so on, and a program to find the class average would have to add up all 32 by name. If a student joins the class, the program must be rewritten.
Real data comes in collections: the marks of a class, the songs in a playlist, the words in a message, the contacts in a phone. Python has a few built-in collection types for exactly this, and together they cover a remarkable share of everyday programming:
- a list keeps values in order, like the marks of a class, and lets you add, remove and change them;
- a string is a sequence of characters, and you'll see it behaves a lot like a list;
- a tuple groups a fixed number of values, like a latitude and a longitude;
- a dictionary connects keys to values, like names to phone numbers, so you can look things up;
- a set holds values with no repeats, like the distinct words in a book.
With loops and functions from the previous lessons, these collections let a few lines of code handle a hundred values or ten million.
Lists
A list is written with square brackets, its items separated by commas. Each item has a position, called its index, and indexes start at 0: the first item is at index 0, the second at index 1, and the last at index len(xs) - 1. Python also lets you count from the end with negative indexes: -1 is the last item, -2 the one before.
Definition— list
A list is an ordered, changeable collection of values. xs[i] is the item at index i; len(xs) is the number of items. Asking for an index that doesn't exist raises an IndexError.
A slice xs[start:stop] makes a new list of the items from index start up to, but not including, stop, exactly like range from Decisions and loops. Leave out start to begin at the front, leave out stop to go to the end, and add a third number for a step: xs[::2] takes every second item, and xs[::-1] is the list reversed.
Note
Why start at 0? Think of the index as "how many items to skip from the front": the first item is 0 steps from the start. This makes several things neat: a list of length n has indexes range(n), and the slices xs[0:k] and xs[k:n] split it into two parts with no overlap and nothing left out. The computer scientist Edsger Dijkstra set out the argument in a short, famous note, Why numbering should start at zero (1982). Not every language agrees: Lua, R and Julia count from 1.
Lists are mutable: you can change them after creating them.
| Code | Effect |
|---|---|
xs[2] = 18 | replace the item at index 2 |
xs.append(26) | add 26 at the end |
xs.insert(0, 20) | insert 20 at index 0, moving the others along |
xs.pop() | remove the last item and return it |
xs.pop(0) | remove the item at index 0 and return it |
xs.remove(19) | remove the first item equal to 19 |
19 in xs | True if 19 is in the list |
Things written after a dot, like xs.append(26), are called methods: functions that belong to a particular type of value and act on it.
To go through a list, loop over it directly. If you need the positions too, enumerate gives you each index together with its item. Built-in functions such as sum, min, max and sorted work on whole lists.
temps = [21, 24, 19, 23, 25] # one week's noon temperatures, in °C
print(temps[0], temps[-1], len(temps))
print(temps[1:4]) # indexes 1, 2, 3
print(temps[::-1]) # reversed copy
temps.append(26)
temps.append(22)
print(temps)
total = 0
for t in temps:
total += t
print("average:", round(total / len(temps), 1))
print("same with sum():", round(sum(temps) / len(temps), 1))
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
for i, t in enumerate(temps):
if t == max(temps):
print("Hottest day:", days[i], t)
print(sorted(temps)) # a new, sorted list
print(temps) # the original is unchangedMutability and aliasing
In Values, variables and expressions you learned to think of a variable as a name tag tied to a value. With numbers, that picture made b = a harmless: numbers can't be changed, only replaced. Lists can be changed, and that makes the picture matter.
a = [1, 2, 3]
b = a # b is tied to the SAME list, not a copy
b.append(4)
print(a) # [1, 2, 3, 4]After b = a, there is one list with two name tags on it. Changing the list through either name changes it for both. This is called aliasing: a and b are aliases, two names for the same object. If you want an independent copy, ask for one: b = a.copy(), b = list(a) or b = a[:].
The same thing happens when you pass a list to a function. The parameter becomes another name for the caller's list, so a function can change a list it is given. Sometimes that is exactly what you want (a function that sorts a list in place); sometimes it is a nasty surprise. A good habit: say in the docstring if a function changes its argument, and otherwise leave the argument alone and return a new list.
def add_bonus_in_place(marks):
"""Add 5 to every mark. Changes the list it is given."""
for i in range(len(marks)):
marks[i] += 5
def with_bonus(marks):
"""Return a new list with 5 added to every mark. The argument is unchanged."""
result = []
for m in marks:
result.append(m + 5)
return result
class_a = [60, 72, 55]
new_marks = with_bonus(class_a)
print(class_a, new_marks) # original untouched
add_bonus_in_place(class_a)
print(class_a) # original changed
x = [1, 2]
y = x
z = x.copy()
x.append(3)
print(x, y, z) # y is an alias; z is a copyNote
One famous trap combines aliasing with default values from Functions. A default is created once, when the function is defined, so def add(item, bag=[]) uses the same list for every call that leaves out bag, and items pile up between calls. The usual fix is bag=None as the default, and if bag is None: bag = [] inside the function.
Strings are sequences too
A string is a sequence of characters, so indexing, slicing, len, in and for loops all work on strings just as on lists: "Nairobi"[0] is "N", "Nairobi"[-3:] is "obi", and for ch in "abc": visits each character in turn.
The big difference is that strings are immutable: you can't change a character in place. name[0] = "J" raises a TypeError. Instead, every string method returns a new string and leaves the original alone. The most useful ones:
| Method | Example | Result |
|---|---|---|
lower, upper | "Lagos".upper() | "LAGOS" |
strip | " hi ".strip() | "hi" (spaces removed from both ends) |
split | "red green blue".split() | ["red", "green", "blue"] |
join | "-".join(["2026", "11", "19"]) | "2026-11-19" |
replace | "a,b,c".replace(",", ";") | "a;b;c" |
find | "banana".find("na") | 2 (index of first match, or -1) |
count | "banana".count("a") | 3 |
startswith | "+254700".startswith("+254") | True |
split and join are a pair worth knowing well: split breaks a string into a list of pieces (at spaces, unless you give a separator such as ","), and join glues a list of strings back together with a separator between them.
Example— Cleaning up a name
People type names in messy ways: " ama MENSAH ". To store it neatly as "Ama Mensah": strip the ends, split into words (which also discards the extra spaces between them), capitalise each word, and join with single spaces. The function below does exactly that, and also builds initials.
def tidy_name(raw):
words = raw.strip().split()
tidy_words = []
for w in words:
tidy_words.append(w[0].upper() + w[1:].lower())
return " ".join(tidy_words)
def initials(name):
result = ""
for word in name.split():
result += word[0] + "."
return result
name = tidy_name(" ama MENSAH ")
print(name, initials(name)) # Ama Mensah A.M.Notice that tidy_name never changes a string: strip, split, upper and lower each produce a new one, and the function collects the new pieces in a list before joining them. Building a list of pieces and joining once at the end is also the efficient way to assemble a long string from many parts.
Strings in Python 3 can hold text in any writing system: "Hà Nội", "नमस्ते" and "東京" are all ordinary strings. One surprise: len counts code points, the units of the Unicode standard, and these don't always match what a reader sees as one letter. len("नमस्ते") is 6, although a Hindi reader sees fewer letters, because some marks that combine with a letter are separate code points. How text becomes bytes is part of Bits and bytes.
Tuples: values that belong together
A tuple is like a list that can't be changed. It is written with parentheses, (14.69, -17.44), and is used for small groups of values that belong together and whose meaning comes from their position: a point's coordinates, a date as (day, month, year), a name and a score.
Tuples can be unpacked into several variables at once:
dakar = (14.69, -17.44) # latitude, longitude
lat, lon = dakar # lat is 14.69, lon is -17.44
a, b = b, a # swap two variables in one lineThis is how a function returns more than one value: it returns a tuple, and the caller unpacks it.
def lowest_and_highest(values):
return min(values), max(values)
low, high = lowest_and_highest([21, 24, 19, 23, 25]) # low = 19, high = 25Dictionaries: looking things up
A list answers the question "what is at position 3?". Very often you want to ask "what goes with this?": the phone number for a name, the capital of a country, the number of times a word appears. That is what a dictionary is for.
Definition— dictionary
A dictionary (dict) is a collection of key–value pairs. Each key appears at most once and is connected to one value. d[key] looks up the value for key, raising a KeyError if the key isn't there; d[key] = value adds a pair or replaces the value.
capitals = {"Kenya": "Nairobi", "Peru": "Lima", "Vietnam": "Hanoi"}
capitals["Kenya"] # 'Nairobi'
capitals["Nepal"] = "Kathmandu" # add a new pair
"Peru" in capitals # True: 'in' checks the keys
capitals.get("Chile", "unknown") # 'unknown': get gives a default instead of an errorLooping over a dictionary goes through its keys; d.items() gives you each key and value together. Keys must be values that can't change, such as strings, numbers or tuples; values can be anything, even lists or other dictionaries.
The single most useful dictionary pattern is counting. Start with an empty dictionary; for each item, add 1 to its count, using get with a default of 0 for items not yet seen:
text = """The rain came to the valley. The farmers smiled,
the children ran to the river, and the river sang all night."""
counts = {}
for word in text.lower().split():
word = word.strip(".,!?")
counts[word] = counts.get(word, 0) + 1
print(counts["the"], counts["river"], counts.get("sun", 0))
for word, n in counts.items():
if n > 1:
print(f"{word!r} appears {n} times")Example— Tracing the counting loop
Take the words the, rain, the. At the start, counts is {}. For the, counts.get("the", 0) is 0, so counts["the"] becomes 1: {"the": 1}. For rain, the same happens: {"the": 1, "rain": 1}. For the second the, counts.get("the", 0) is now 1, so counts["the"] becomes 2: {"the": 2, "rain": 1}. Each word costs one lookup and one update, however many different words have been seen before.
How does Python find a key so quickly, even in a dictionary with millions of entries? Not by checking every key one by one. Dictionaries are built on a structure called a hash table, which you'll study (and build yourself) in Hash tables. For now it's enough to know that looking up a key, adding a pair and checking in take about the same short time whether the dictionary holds ten entries or ten million.
Sets and comprehensions
A set is an unordered collection with no repeats, written with curly braces: {"red", "green"}. (An empty set is written set(), because {} is an empty dictionary.) Adding something that is already there changes nothing, so set(xs) is a quick way to find the distinct items of a list. Sets support the operations of Lumenary Math's Sets: a | b (union), a & b (intersection) and a - b (difference). Like dictionary keys, set membership tests with in are fast.
A list comprehension builds a list from another sequence in one line. These two pieces of code do the same thing:
squares = []
for n in range(1, 11):
if n % 2 == 0:
squares.append(n * n)
squares = [n * n for n in range(1, 11) if n % 2 == 0]Read the comprehension from the middle: for each n in the range, if n is even, take n * n. There are dictionary and set comprehensions too, with curly braces. Comprehensions are elegant for simple transformations and filters; when one grows past a single clear line, an ordinary loop is kinder to the reader.
visitors_mon = ["Ana", "Ravi", "Li", "Ana", "Kwame"]
visitors_tue = ["Li", "Fatima", "Ravi"]
mon = set(visitors_mon)
tue = set(visitors_tue)
print(len(visitors_mon), "visits, but", len(mon), "different people on Monday")
print("both days:", sorted(mon & tue))
print("either day:", sorted(mon | tue))
print("only Monday:", sorted(mon - tue))
evens_squared = [n * n for n in range(1, 11) if n % 2 == 0]
print(evens_squared)
words = "the quick brown fox jumps over the lazy dog".split()
lengths = {w: len(w) for w in words}
long_words = [w.upper() for w in words if len(w) >= 5]
print(lengths)
print(long_words)(Sets have no order, so the program uses sorted to print them in a predictable order.)
Choosing the right collection
Professional programmers choose a collection by asking what they need to do with it most often:
| You need to… | Use | Why |
|---|---|---|
| keep items in order, add and remove them | list | ordered, changeable, indexed by position |
| group a fixed number of related values | tuple | unchangeable, so it can be a dictionary key |
| look things up by a key, or count | dictionary | fast lookup by key |
| remove duplicates, or test membership fast | set | no repeats, fast in |
The choice affects speed, not only convenience. Checking x in some_list means looking through the list item by item, which takes longer the longer the list is; x in some_set does not. Measuring exactly how much longer is the subject of Measuring the cost of a program, and the rest of the Core level builds these structures from the inside.
Common mistakes
Misconception
"The first item is xs[1]." Indexes start at 0, so the first item is xs[0] and the last is xs[len(xs) - 1], or simply xs[-1]. xs[len(xs)] raises an IndexError.
Misconception
xs = xs.sort(). The method sort sorts the list in place and returns None, so this line throws the sorted list away and leaves xs as None. Either call xs.sort() on its own line, or use ys = sorted(xs), which returns a new sorted list.
Misconception
"b = a makes a copy of the list." It makes a second name for the same list. Use a.copy() when you want an independent copy.
Misconception
"I can fix a typo with word[0] = 'J'." Strings are immutable. Build a new string instead: word = "J" + word[1:].
Misconception
Removing items from a list while looping over it. The loop keeps its place by index, so removing items shifts the rest and some get skipped. Build a new list of the items you want to keep instead, for example with a comprehension.
Exercises
1. Lists, strings and dictionaries
Warm-upComputationalWhat is the value of xs[-2] + xs[1]?
xs = [4, 8, 15, 16, 23, 42]
2. Lists, strings and dictionaries
Warm-upComputationalWhat string is "banana"[1:4]? Type the characters without quotation marks.
3. Lists, strings and dictionaries
CoreComputationalWhat does this program print?
shopping = ["rice", "beans"]
my_copy = shopping
my_copy.append("plantain")
print(len(shopping))
4. Lists, strings and dictionaries
CoreComputationalKemal wants a sorted list of marks and writes:
marks = [72, 55, 90, 61]
marks = marks.sort()
print(marks)
What is printed?
5. Lists, strings and dictionaries
CoreComputationalWrite word_counts(text) that returns a dictionary mapping each word to the number of times it appears in text. Treat upper and lower case as the same (store words in lowercase), and remove the punctuation characters .,!?;: from the ends of each word.
For example, word_counts("Go, go, GO! Stop.") should be {"go": 3, "stop": 1}.
6. Lists, strings and dictionaries
CoreComputationalWrite unique_in_order(items) that returns a new list with the duplicates removed, keeping the first appearance of each item in its original position. For example:
unique_in_order([3, 1, 3, 2, 1]) == [3, 1, 2]
The function must not change the list it is given. (Hint: set(items) alone loses the order.)
7. Lists, strings and dictionaries
CoreComputationalA palindrome reads the same forwards and backwards, like level or Step on no pets. Write is_palindrome(phrase) that returns True if phrase is a palindrome when you ignore case and everything that is not a letter, and False otherwise.
Useful tools: ch.isalpha() is True for letters; s[::-1] is s reversed.
8. Lists, strings and dictionaries
StretchComputationalWrite group_by_length(words) that returns a dictionary whose keys are word lengths and whose values are lists of the words of that length, in the order they appeared. For example:
group_by_length(["sun", "moon", "sky", "star", "a"])
# {3: ["sun", "sky"], 4: ["moon", "star"], 1: ["a"]}
Further reading and resources
- The Python Tutorial: Data structures · Python Software FoundationLists, tuples, sets, dictionaries and comprehensions.
- String methods · Python Software FoundationThe full list, from capitalize to zfill.
- Mapping types: dict · Python Software Foundation
- Design and history FAQ: How are lists implemented in CPython? · Python Software FoundationA short official answer, for the curious.