Python Data Structures

Lists, tuples, dictionaries, sets, deque, Counter, defaultdict, heapq, and collection idioms.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

Lists

List operations and idioms.

Append item

Append item

pythonANYlistappend
python
items.append(value)

Extend list

Extend list

pythonANYlistextend
python
items.extend([4, 5, 6])

Insert at index

Insert at index

pythonANYlistinsert
python
items.insert(0, value)

Slice a list

Slice a list

pythonANYlistslice
python
subset = items[1:4]

Reverse slice copy

Reverse slice copy

pythonANYlistslicereverse
python
reversed_items = items[::-1]

Sort in place

Sort in place

pythonANYlistsort
python
items.sort(key=str.lower, reverse=False)

Sorted copy

Sorted copy

pythonANYlistsort
python
ordered = sorted(items)

Remove by value

Remove by value

pythonANYlistremove
python
items.remove(value)

Pop last item

Pop last item

pythonANYlistpop
python
last = items.pop()

Count occurrences

Count occurrences

pythonANYlistcount
python
count = items.count(value)

Dictionaries

Dictionary lookups, merging, and comprehensions.

Get with default

Get with default

pythonANYdictget
python
theme = settings.get("theme", "light")

Set default

Set default

pythonANYdictsetdefault
python
items = grouped.setdefault(category, [])

Merge dictionaries

Merge dictionaries

pythonANYdictmerge
python
merged = defaults | overrides

Dictionary update

Dictionary update

pythonANYdictmerge
python
config |= extra_config

Iterate keys and values

Iterate keys and values

pythonANYdictitems
python
for key, value in data.items():
    print(key, value)

Dictionary comprehension

Dictionary comprehension

pythonANYdictcomprehension
python
squares = {n: n * n for n in range(5)}

Invert mapping

Invert mapping

pythonANYdictcomprehension
python
inverse = {value: key for key, value in mapping.items()}

Sets and tuples

Set algebra and tuple-style records.

Set union

Set union

pythonANYsetunion
python
combined = a | b

Set intersection

Set intersection

pythonANYsetintersection
python
common = a & b

Set difference

Set difference

pythonANYsetdifference
python
only_a = a - b

Symmetric difference

Symmetric difference

pythonANYsetdifference
python
changed = a ^ b

Is subset

Is subset

pythonANYsetsubset
python
is_subset = a <= b

Named tuple alternative

Named tuple alternative

pythonANYtuplecollections
python
from collections import namedtuple
Point = namedtuple("Point", ["x", "y"])
p = Point(10, 20)

Queues, heaps, and specialized containers

Standard-library collection helpers.

Deque as queue

Deque as queue

pythonANYdequequeue
python
from collections import deque
queue = deque(["a", "b"])
queue.append("c")
item = queue.popleft()

Counter frequencies

Counter frequencies

pythonANYcountercollections
python
from collections import Counter
counts = Counter(words)
common = counts.most_common(3)

Defaultdict list grouping

Defaultdict list grouping

pythonANYdefaultdictcollections
python
from collections import defaultdict
groups = defaultdict(list)
for user in users:
    groups[user["role"]].append(user)

Heap push and pop

Heap push and pop

pythonANYheapqpriority-queue
python
import heapq
heap = []
heapq.heappush(heap, 5)
smallest = heapq.heappop(heap)