Data Structures cheat sheet
96 one-line examples for Data Structures. Open it while you code, or print it and keep it near the keyboard. Free, with no signup and no ads.
Arrays
| Code | What it does |
|---|---|
a = [3, 1, 2] | Make a list with three numbers. |
a[0] | Read the first item. Index starts at zero. |
a[-1] | Read the last item. |
a[1] = 9 | Change the second item to 9. |
len(a) | Return the number of items. |
a.append(4) | Add one item at the end. |
a.insert(0, 7) | Add 7 at the front. |
a.pop() | Remove and return the last item. |
a.pop(0) | Remove and return the first item. |
a[1:3] | Copy items from index 1 up to 2. |
for x in a: print(x) | Visit each item in order. |
a.sort() | Sort the list in place. |
Strings
| Code | What it does |
|---|---|
s = "hello" | Make a string of five characters. |
s[0] | Read the first character. |
s[1:4] | Copy characters 1, 2, and 3. |
s.upper() | Return a copy in upper case. |
s.lower() | Return a copy in lower case. |
s.strip() | Remove spaces at both ends. |
s.split(",") | Split the string at each comma. |
"-".join(parts) | Join parts with a dash between them. |
s.replace("l", "L") | Return a copy with l changed to L. |
s.find("ll") | Return the index of the first match. |
"ll" in s | Test if the string contains ll. |
len(s) | Return the number of characters. |
Dictionaries
| Code | What it does |
|---|---|
d = {"a": 1, "b": 2} | Make a dictionary with two pairs. |
d["a"] | Read the value for key a. |
d["c"] = 3 | Add a new key and value. |
d.get("z", 0) | Read key z or return 0. |
"a" in d | Test if the key a exists. |
d.keys() | Return a view of all keys. |
d.values() | Return a view of all values. |
d.items() | Return a view of key and value pairs. |
for k in d: print(k) | Visit each key in the dictionary. |
d.pop("a") | Remove key a and return its value. |
del d["b"] | Remove key b from the dictionary. |
len(d) | Return the number of pairs. |
Stacks
| Code | What it does |
|---|---|
s = [] | Make an empty stack with a list. |
s.append(1) | Push 1 onto the top of the stack. |
s[-1] | Look at the top item. |
s.pop() | Pop the top item off the stack. |
len(s) == 0 | Test if the stack is empty. |
s = [1, 2, 3] | Make a stack with 3 on top. |
s.append(4) | Push 4. The top is now 4. |
x = s.pop() | Pop the top item into x. |
while s: s.pop() | Empty the stack one item at a time. |
s.clear() | Remove all items from the stack. |
s[-2] | Look at the item below the top. |
len(s) | Return the number of items on the stack. |
Queues
| Code | What it does |
|---|---|
from collections import deque | Import the deque class for fast queues. |
q = deque() | Make an empty queue. |
q.append(1) | Add 1 at the back of the queue. |
q.popleft() | Remove and return the front item. |
q[0] | Look at the front item. |
q[-1] | Look at the back item. |
len(q) == 0 | Test if the queue is empty. |
q.appendleft(0) | Add 0 at the front of the queue. |
q.pop() | Remove and return the back item. |
while q: q.popleft() | Empty the queue from the front. |
q.clear() | Remove all items from the queue. |
len(q) | Return the number of items in the queue. |
Linked Lists
| Code | What it does |
|---|---|
class Node: | Start a class for one list node. |
def __init__(self, v): | Define the node setup method. |
self.val = v | Store the value in the node. |
self.next = None | Set the next link to empty. |
n = Node(5) | Make one node with value 5. |
n.next = Node(7) | Link a new node after n. |
head = n | Mark n as the first node. |
p = head | Start a pointer at the head. |
while p: p = p.next | Walk the list to the end. |
p.next = Node(9) | Add a node at the end. |
head = head.next | Remove the first node. |
p.next = p.next.next | Skip the node after p. |
Sets
| Code | What it does |
|---|---|
s = {1, 2, 3} | Make a set with three numbers. |
s.add(4) | Add 4 to the set. |
s.remove(1) | Remove 1. Raise an error if missing. |
s.discard(1) | Remove 1. Do nothing if missing. |
2 in s | Test if 2 is in the set. |
a | b | Return the union of sets a and b. |
a & b | Return the items in both sets. |
a - b | Return items in a but not in b. |
a ^ b | Return items in one set only. |
set([1, 1, 2]) | Make a set. Duplicates drop out. |
len(s) | Return the number of items. |
s.clear() | Remove all items from the set. |
Trees
| Code | What it does |
|---|---|
class Node: | Start a class for one tree node. |
self.left = None | Set the left child link to empty. |
self.right = None | Set the right child link to empty. |
root = Node(10) | Make a root node with value 10. |
root.left = Node(5) | Add a left child with value 5. |
root.right = Node(15) | Add a right child with value 15. |
def walk(n): | Define a function to visit nodes. |
if n: print(n.val) | Print the value if the node exists. |
walk(n.left) | Visit the left child first. |
walk(n.right) | Visit the right child next. |
walk(root) | Start the walk at the root. |
if not n: return | Stop when the node is empty. |