Two servers, two log files, same time window. You want one merged timeline so you can see what happened across the whole system. What's the cleanest Python?
Concatenate the two lists and sort the result by timestamp. Both operations are one-liners.
Exactly. List concat with + builds a new list. sorted with a key function orders by any field you pick:
merged = stream_a + stream_b
return sorted(merged, key=lambda log: log["timestamp"])Two lines. Works for empty inputs — concatenating with [] returns the other list unchanged, and sorting [] returns [].
Why does lexicographic sort work on ISO timestamp strings? Doesn't time need parsing?
ISO 8601 was designed so that alphabetical and chronological order match. "2026-04-14 10:00:00" is less than "2026-04-14 11:00:00" as a string compare, character by character. So sorting the raw strings gives correct chronological order with zero parsing cost.
Is sorting the combined list wasteful? Each stream is already sorted on its own.
It is, technically. For huge pre-sorted inputs the right tool is heapq.merge(a, b, key=...) — O(n+m) instead of O(n log n). For typical log analysis sizes, the concat-and-sort version is simpler, readable, and fast enough. Optimise when you measure a problem, not before:
import heapq
merged = heapq.merge(stream_a, stream_b, key=lambda log: log["timestamp"])The empty-input handling is free — no special cases. That's my favourite kind of function.
A function that handles every edge case via the natural behaviour of its primitives is a well-designed function. merge_log_streams is that shape.
sorted(key=lambda) for multi-list mergeTL;DR: concatenate with +, sort with a key function, done.
a + b — new list, empty-safesorted(xs, key=fn) — sorts by fn(x) without modifying inputreverse=True — descending orderheapq.merge| Size | Tool |
|---|---|
| < 100k | sorted(a + b, key=...) |
| > 1M, pre-sorted | heapq.merge(a, b, key=...) |
Sort is stable — equal keys keep their relative insertion order.
Write `merge_log_streams(stream_a, stream_b)` that takes two lists of log dicts (each with a `timestamp` key) and returns one list sorted chronologically. Use `stream_a + stream_b` and `sorted(..., key=lambda log: log["timestamp"])`.
Tap each step for scaffolded hints.
No blank-editor panic.
Two servers, two log files, same time window. You want one merged timeline so you can see what happened across the whole system. What's the cleanest Python?
Concatenate the two lists and sort the result by timestamp. Both operations are one-liners.
Exactly. List concat with + builds a new list. sorted with a key function orders by any field you pick:
merged = stream_a + stream_b
return sorted(merged, key=lambda log: log["timestamp"])Two lines. Works for empty inputs — concatenating with [] returns the other list unchanged, and sorting [] returns [].
Why does lexicographic sort work on ISO timestamp strings? Doesn't time need parsing?
ISO 8601 was designed so that alphabetical and chronological order match. "2026-04-14 10:00:00" is less than "2026-04-14 11:00:00" as a string compare, character by character. So sorting the raw strings gives correct chronological order with zero parsing cost.
Is sorting the combined list wasteful? Each stream is already sorted on its own.
It is, technically. For huge pre-sorted inputs the right tool is heapq.merge(a, b, key=...) — O(n+m) instead of O(n log n). For typical log analysis sizes, the concat-and-sort version is simpler, readable, and fast enough. Optimise when you measure a problem, not before:
import heapq
merged = heapq.merge(stream_a, stream_b, key=lambda log: log["timestamp"])The empty-input handling is free — no special cases. That's my favourite kind of function.
A function that handles every edge case via the natural behaviour of its primitives is a well-designed function. merge_log_streams is that shape.
sorted(key=lambda) for multi-list mergeTL;DR: concatenate with +, sort with a key function, done.
a + b — new list, empty-safesorted(xs, key=fn) — sorts by fn(x) without modifying inputreverse=True — descending orderheapq.merge| Size | Tool |
|---|---|
| < 100k | sorted(a + b, key=...) |
| > 1M, pre-sorted | heapq.merge(a, b, key=...) |
Sort is stable — equal keys keep their relative insertion order.