Python Cheatsheet
The everyday Python syntax that comes up constantly — data structures, comprehensions, slicing and the handy built-ins.
Lists
Add an item to the end.
Append all items from another iterable.
Remove and return item at index i.
Return a new sorted list.
Number of items.
Dicts
Safe lookup with a fallback.
Iterate key-value pairs.
Just the keys / values.
Get k, inserting a default if absent.
Merge two dicts into a new one.
Comprehensions
List comprehension — transform.
With a filter condition.
Dict comprehension.
Set comprehension (unique values).
Slicing
Items 1 up to (not including) 4.
Every second item.
Reversed.
Last item.
Strings & built-ins
Join an iterable into a string.
Split a string into a list.
f-string interpolation.
Loop with index and value.
Pair up two iterables.