Useful One-Liners
Introduction
These one-liners are not code golf (writing the shortest code possible at the expense of readability). They are established Python idioms that interviewers love to see.
1. Flatten a 2D Matrix
If you have a matrix and need to process all elements as a flat list, you can use a nested list comprehension.
matrix = [[1, 2], [3, 4]]
# The One-Liner
flat = [val for row in matrix for val in row]
print(flat) # [1, 2, 3, 4]
2. Initialize a 2D Matrix (Safely)
If a DP problem or graph problem requires a 2D grid, you must initialize it correctly.
DANGER: matrix = [[0] * COLS] * ROWS creates references to the same inner list. Modifying matrix[0][0] will also modify matrix[1][0].
3. Matrix Transposition
If you need to rotate a grid or process it column-by-column, transpose it.
matrix = [[1, 2], [3, 4]]
# The One-Liner
transposed = list(zip(*matrix))
print(transposed) # [(1, 3), (2, 4)]
4. Conditional Assignment (Ternary Operator)
Instead of a 4-line if/else block just to assign a value.
5. Find First Match or Default
If you want to find the first item in a list that matches a condition, use next() with a generator expression.
users = [
{"id": 1, "status": "offline"},
{"id": 2, "status": "online"}
]
# Find first online user, or return None if nobody is online
first_online = next((u for u in users if u["status"] == "online"), None)
6. Any / All
To check if a grid is completely valid, or if at least one condition is met, without writing a manual for loop with a return False inside.
7. Merge Two Dictionaries
If you need to combine two state dictionaries (Python 3.9+).