Skip to content

Data Structures

Lists, dictionaries, sets and tuples — what each is for, and how to work with it.

Choosing a container

NeedUse
an ordered collection you will changelist
lookup by name or iddict
uniqueness, or fast "is it in here?"set
a fixed group of values that belong togethertuple

Lists

items = ["a", "b", "c"]
items[0]      # "a" — indexes start at zero
items[-1]     # "c" — counts from the end
len(items)    # 3

Slicing

items[1:3]     # from 1, up to but not including 3
items[:2]      # from the start
items[2:]      # to the end
items[:]       # a copy
items[::2]     # every second item
items[::-1]    # reversed copy

Methods

MethodEffectReturns
.append(x)add to the endNone
.extend(other)add every item of another listNone
.insert(i, x)add at position iNone
.remove(x)delete the first xNone
.pop() / .pop(i)remove and returnthe item
.sort()sort in placeNone
.reverse()reverse in placeNone
.count(x)how many xint
.index(x)position of first xint
sorted(items)                 # a NEW sorted list
sorted(items, reverse=True)   # descending
sorted(items, key=len)        # sort by a computed value
sum(nums), min(nums), max(nums)

The distinction that catches people: .sort() changes the list and returns None; sorted() leaves it alone and returns a new one.

Iterating

for item in items:
for i, item in enumerate(items):          # index and value
for i, item in enumerate(items, start=1): # numbering from one
for a, b in zip(list_a, list_b):          # two lists together

Comprehensions

[n * 2 for n in nums]                 # transform
[n for n in nums if n > 0]            # filter
[n * 2 for n in nums if n > 0]        # both

Dictionaries

person = {"name": "Ada", "age": 36}
person["name"]                 # KeyError if missing
person.get("job")              # None if missing
person.get("job", "unknown")   # your own default
person["city"] = "London"      # add or replace
del person["age"]              # remove
"name" in person               # checks KEYS, not values

Iterating

for key in person:
for value in person.values():
for key, value in person.items():     # the one you want most of the time

Idioms

counts[word] = counts.get(word, 0) + 1        # counting
groups.setdefault(key, []).append(value)      # grouping
{k: v for k, v in d.items() if v > 0}         # dict comprehension

Sets

tags = {"python", "beginner"}
empty = set()          # {} makes an empty DICT
tags.add("web")
tags.discard("web")    # no error if absent
OperatorMeaning
a | bunion — in either
a & bintersection — in both
a - bdifference — in a only
a ^ bin exactly one

No order and no indexing. To de-duplicate while keeping order:

seen = set()
result = [x for x in items if not (x in seen or seen.add(x))]

Tuples

point = (3, 4)
pair = 3, 4          # brackets optional
single = (3,)        # the comma makes it a tuple
x, y = point         # unpacking
a, b = b, a          # swap

Immutable, so they can be dictionary keys and set members — lists cannot.

Nested data

people = [{"name": "Ada", "age": 36}]
people[0]["name"]                  # read left to right

config = {"server": {"port": 8080}}
config["server"]["port"]

Copying

shallow = items[:]          # or list(items)
import copy
deep = copy.deepcopy(nested)   # when the contents are themselves containers

b = a does not copy — both names point at the same list.

This cheatsheet is the summary. If you want to build it yourself, the Data Structures course walks you through it in the browser — the first lesson is free.