Python Fundamentals for LeetCode & DSA Interviews
Python’s expressive syntax and rich standard library make it ideal for coding interviews.1. Comprehensions & Unpacking
List Comprehensions
Dict & Set Comprehensions
Tuple Unpacking
Generators
2. Iteration & Functional Tools
enumerate()
Definition: enumerate() returns pairs of (index, value) from an iterable, allowing you to loop with both the index and the value.
zip()
Definition: zip() aggregates elements from two or more iterables (like lists or tuples), returning tuples with one element from each iterable.
map() and filter()
Definition: map() applies a function to every item in an iterable and returns a map object (which can be converted to a list). filter() filters items in an iterable based on a function that returns True or False.
any() and all()
Definition: any() returns True if at least one element in the iterable is true. all() returns True if all elements in the iterable are true.
sorted(), reversed(), and sum()/min()/max()
Definition: sorted() returns a new sorted list from the items in an iterable. reversed() returns an iterator that accesses the given sequence in the reverse order. sum(), min(), and max() return the sum, minimum, and maximum of an iterable, respectively.
range()
Definition: range() generates a sequence of numbers, commonly used for looping a specific number of times in for-loops.
- Remember:
range(n)→0..n-1
itertools.chain()
Definition: itertools.chain() takes multiple iterables and returns a single iterator that produces items from the first iterable until it is exhausted, then continues to the next iterable, and so on.
3. Data Structures & Libraries
collections.Counter
Definition: collections.Counter is a dict subclass for counting hashable objects. It returns a dictionary where elements are stored as keys and their counts as values.
collections.defaultdict
Definition: collections.defaultdict is a dict subclass that provides a default value for nonexistent keys, avoiding KeyError.
collections.deque
Definition: collections.deque is a double-ended queue that supports fast appends and pops from both ends.
heapq (min-heap)
Definition: The heapq module provides an implementation of the min-heap queue algorithm, also known as the priority queue algorithm.
bisect
Definition: The bisect module provides support for maintaining a list in sorted order without having to sort the list after each insertion.
4. Pythonic Idioms & Techniques
Sorting with key=
Lambda expressions
enumerate() + zip() unpacking
functools.lru_cache for memoization
Definition: functools.lru_cache is a decorator that caches the results of function calls, so that when the same inputs occur again, the cached result is returned instead of recomputing.
5. Common Pitfalls
Mutable Default Arguments
Shallow vs Deep Copy
Off-by-One Errors
range(n)is0..n-1range(len(arr))is correct- Be careful with
<=vs<
Integer Division
List Multiplication Pitfall
is vs ==
6. Best Practices
- Use comprehensions (
[x for x in ...]) - Prefer built-ins:
Counter,min,max,sum,sorted - Use
defaultdictfor grouping - Use
heapq.nlargest/nsmallestfor “top K” problems - Sort with
key=function - Cache expensive calls with
@lru_cache - Avoid unnecessary loops with
map,filter,zip,enumerate - Keep code readable and Pythonic