Python Cheatsheet
44 essential methods for coding interviews
Showing 44 of 44 methods
| Method | Syntax | Description | Time | Priority |
|---|---|---|---|---|
list.append | list.append(item) | Add an item to the end of the list | O(1) | essential |
list.pop | list.pop([index]) | Remove and return item at index (default last) | O(1) end, O(n) middle | essential |
list.sort | list.sort(key=None, reverse=False) | Sort list in place | O(n log n) | essential |
list.reverse | list.reverse() | Reverse list in place | O(n) | essential |
list slicing | list[start:end:step] | Extract a portion of the list | O(k) | essential |
str.split | str.split(sep=None, maxsplit=-1) | Split string into list by separator | O(n) | essential |
str.join | separator.join(iterable) | Join iterable elements with separator | O(n) | essential |
str.strip | str.strip([chars]) | Remove leading/trailing whitespace or specified chars | O(n) | essential |
str.replace | str.replace(old, new, count=-1) | Return copy with all occurrences replaced | O(n) | essential |
str.lower / str.upper | str.lower() / str.upper() | Convert string to lowercase/uppercase | O(n) | essential |
ord / chr | ord(char) / chr(code) | Convert between character and ASCII/Unicode code point | O(1) | essential |
dict.get | dict.get(key, default=None) | Get value for key, or default if not found | O(1) | essential |
dict.keys | dict.keys() | Return view of dictionary keys | O(1) | essential |
dict.values | dict.values() | Return view of dictionary values | O(1) | essential |
dict.items | dict.items() | Return view of (key, value) pairs | O(1) | essential |
defaultdict | defaultdict(default_factory) | Dict subclass with default value for missing keys | O(1) | essential |
Counter | Counter(iterable) | Dict subclass for counting hashable objects | O(n) | essential |
set.add | set.add(elem) | Add element to set | O(1) | essential |
set union | set1 | set2 or set1.union(set2) | Return set with elements from both sets | O(n + m) | essential |
set intersection | set1 & set2 or set1.intersection(set2) | Return set with elements common to both | O(min(n, m)) | essential |
len | len(obj) | Return the length of an object | O(1) | essential |
range | range(start, stop, step) | Generate sequence of numbers | O(1) creation | essential |
enumerate | enumerate(iterable, start=0) | Return iterator of (index, value) pairs | O(1) creation | essential |
zip | zip(*iterables) | Iterate over multiple iterables in parallel | O(1) creation | essential |
sorted | sorted(iterable, key=None, reverse=False) | Return new sorted list from iterable | O(n log n) | essential |
list comprehension | [expr for item in iterable if condition] | Create list using concise syntax | O(n) | essential |
min / max | min(iterable, key=None) / max(iterable, key=None) | Return smallest/largest item | O(n) | essential |
sum | sum(iterable, start=0) | Sum all items in iterable | O(n) | essential |
abs | abs(x) | Return absolute value of number | O(1) | essential |
heapq.heappush | heapq.heappush(heap, item) | Push item onto heap maintaining heap property | O(log n) | essential |
heapq.heappop | heapq.heappop(heap) | Pop smallest item from heap | O(log n) | essential |
heapq.heapify | heapq.heapify(list) | Transform list into heap in-place | O(n) | essential |
deque | deque(iterable, maxlen=None) | Double-ended queue with O(1) operations at both ends | O(1) append/pop both ends | essential |
bisect.bisect_left | bisect.bisect_left(arr, x) | Find insertion point for x in sorted array | O(log n) | essential |
list.extend | list.extend(iterable) | Extend list by appending all items from an iterable | O(k) | common |
str.find | str.find(sub, start=0, end=len) | Return lowest index of substring, or -1 if not found | O(n*m) | common |
str.isalnum | str.isalnum() | Check if all characters are alphanumeric | O(n) | common |
dict.update | dict.update(other) | Update dict with key/value pairs from another dict | O(k) | common |
set.remove / discard | set.remove(elem) / set.discard(elem) | Remove element from set | O(1) | common |
set difference | set1 - set2 or set1.difference(set2) | Return set with elements in set1 but not set2 | O(n) | common |
map | map(function, iterable) | Apply function to every item of iterable | O(1) creation | common |
filter | filter(function, iterable) | Filter items where function returns True | O(1) creation | common |
any / all | any(iterable) / all(iterable) | Check if any/all elements are truthy | O(n) | common |
reversed | reversed(seq) | Return reverse iterator | O(1) creation | common |