1/76
A comprehensive set of Q&A flashcards covering core list, string, dict, set, deque, heapq, collections, itertools, and math operations, bitwise tricks, recursion helpers, unpacking patterns, comprehensions, and Pythonic idioms from the Ultimate Python Cheat Sheet for LeetCode prep.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
What does list.append(x) do?
Adds item x to the end of the list.
How does list.extend(iterable) modify a list?
Appends each element from the iterable to the end of the list.
What is the effect of list.insert(i, x)?
Inserts x at index i, shifting subsequent items right.
How does list.remove(x) work?
Removes the first occurrence of value x from the list.
What value does list.pop([i]) return?
Removes and returns the item at index i (last item if i is omitted).
What does list.reverse() do?
Reverses the elements of the list in place.
How does list.sort() affect a list?
Sorts the list in place in ascending order by default.
What is the purpose of list.copy()?
Returns a shallow copy of the list.
What does str.split(sep=None) return?
A list of substrings split on sep (whitespace if sep is None).
How is str.join(iterable) used?
Concatenates the strings in iterable, placing the original string as the separator.
What does str.strip() remove?
Leading and trailing whitespace (or specified characters).
How does str.replace(old, new) modify a string?
Returns a copy with all occurrences of old replaced by new.
What does str.find(sub) output?
Lowest index where substring sub is found, or -1 if absent.
What check does str.startswith(prefix) perform?
Returns True if the string begins with prefix.
What is returned by str.upper()?
An uppercase version of the string.
How does dict.get(key, default=None) behave?
Returns value for key if present, else default.
What does dict.items() provide?
A view of the dictionary’s key–value pairs.
How does dict.pop(key) work?
Removes key and returns its value; raises KeyError if key absent.
What is dict.popitem() used for?
Removes and returns the last inserted key–value pair (LIFO).
How does dict.update(other) modify a dictionary?
Merges key–value pairs from other, overwriting existing keys.
What does dict.setdefault(key, default) do?
Returns value if key exists; otherwise inserts key with default and returns default.
How does set.add(elem) modify a set?
Adds elem to the set if not already present.
What is the difference between set.remove(elem) and set.discard(elem)?
remove raises KeyError if elem absent; discard silently does nothing.
How is set.union(other) used?
Returns a new set containing elements from both sets.
What result does set.intersection(other) give?
A set of elements common to both sets.
What does set.difference(other) return?
Elements present in the first set but not in other.
What does set.issubset(other) test?
True if every element of the set is also in other.
What does len(obj) provide in Python?
The number of items in obj (for sequences, mappings, etc.).
How does sum(iterable) operate?
Returns the total of the items in the iterable (optionally plus a start value).
What does sorted(iterable) return?
A new list containing the items from iterable in ascending order.
Why is enumerate(iterable) useful?
Yields (index, value) pairs while iterating over iterable.
What does zip(*iterables) generate?
Tuples that aggregate items from each iterable by position.
How is map(func, iterable) commonly used?
Applies func to each item, producing an iterator of results.
What does filter(func, iterable) output?
An iterator with items for which func(item) is true.
What does any(iterable) test?
Returns True if at least one element is truthy.
What does all(iterable) verify?
Returns True only if every element is truthy.
What iterator is produced by reversed(seq)?
An iterator that yields the elements of seq in reverse order.
How does deque.appendleft(x) differ from deque.append(x)?
appendleft adds x to the left end; append adds to the right end.
What happens when deque.popleft() is called?
Removes and returns the leftmost item of the deque.
How does deque.extend(iterable) behave?
Adds all elements of iterable to the right side of the deque.
What is the effect of deque.rotate(n)?
Rotates n steps to the right (left if n is negative).
What does heapq.heapify(lst) accomplish?
Reorders lst in-place into a min-heap structure.
When would you use heapq.heappush(heap, item)?
To push item onto the heap while maintaining heap order.
What is returned by heapq.heappop(heap)?
The smallest item from the heap, after removing it.
How does heapq.nlargest(n, iterable) work?
Returns a list with the n largest elements from iterable.
What does heapq.heapreplace(heap, item) do?
Pops and returns smallest item, then pushes new item onto heap.
What is collections.Counter(iterable) useful for?
Counts the frequency of elements, returning a dict-like object.
How does collections.defaultdict(type) differ from dict?
Automatically creates default values of the given type for missing keys.
Why use collections.OrderedDict?
Maintains insertion order of keys (standard dicts do since Python 3.7, but OrderedDict adds extra methods).
What does itertools.product(*iterables) generate?
The Cartesian product of input iterables as tuples.
How does itertools.permutations(iterable, r) differ from combinations?
Permutations consider order; combinations do not.
What does itertools.combinations(iterable, r) return?
r-length subsequences of elements in sorted order, without replacement.
Why use itertools.accumulate(iterable)?
Produces running totals (or results of another binary function).
What does itertools.groupby(iterable) accomplish?
Groups consecutive identical elements after optional sorting/key-func.
How does itertools.chain(*iterables) help iteration?
Flattens multiple iterables into a single sequential iterator.
What is math.gcd(a, b) used for?
Computes the greatest common divisor of a and b.
How does math.lcm(a, b) differ from gcd?
Returns the least common multiple of a and b.
What does math.factorial(n) compute?
n! (the product of all positive integers ≤ n).
When would you call math.sqrt(x)?
To obtain the square root of x as a float.
What is math.ceil(x) contrasted with math.floor(x)?
ceil rounds x up to the nearest integer; floor rounds down.
What do math.comb(n, k) and math.perm(n, k) calculate?
comb gives the number of combinations; perm gives the number of permutations.
How can x & (x - 1) be used with powers of two?
It clears the lowest set bit, yielding 0 if and only if x is a power of 2.
What does the << operator perform in Python?
A left bit shift, equivalent to multiplying by 2**n.
How is the ^ (XOR) operator handy in coding interviews?
It can isolate unique/non-duplicated numbers in pairs-of-duplicates arrays.
What effect does the ~ operator have on an integer?
Bitwise NOT: flips every bit (two’s-complement).
How does @lru_cache(maxsize=None) aid recursion?
Memoizes function calls, caching results to avoid recomputation.
Why call sys.setrecursionlimit(n)?
To raise the maximum depth of the Python call stack for deep recursion.
What does (a, b) = (b, a) achieve?
Swaps the values of a and b without a temporary variable.
Explain a, *rest = iterable destructuring.
Assigns the first element to a and the remaining elements as a list to rest.
Give the pattern for a list comprehension with a condition.
[x for x in iterable if condition].
How is a dictionary comprehension written?
{k: v for k, v in iterable}.
What does "if not lst:" check in Pythonic code?
Tests whether the list lst is empty (or falsy).
Why use -value with heapq to create a max-heap?
Pushing negative values into a min-heap makes it behave like a max-heap.
How can you reverse a string s most Pythonically?
''.join(reversed(s)) or s[::-1].
How do you sort a dictionary by its values?
sorted(dict.items(), key=lambda x: x[1]).
What is a common fast pattern for reading space-separated ints?
list(map(int, input().split())).
How are type hints added to a function returning int from List[int]?
def func(nums: List[int]) -> int: