Code golf combining multiple sorted lists into a single sorted list
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Combining multiple sorted lists into one sorted output is a classic merge problem. In code golf, shortest syntax wins, but a compact solution is only useful if it is still correct on duplicates, empty inputs, and uneven list lengths. Knowing both golf-style and algorithmic approaches helps you choose the right tradeoff for your context.
Start with the Golf-Friendly Baseline
The shortest approach in many languages is flatten then sort.
This is concise and correct for any input shape where elements are comparable. Complexity is based on total element count and full sort cost. For small and medium inputs, this can be perfectly acceptable and very readable.
Use Heap-Based K-Way Merge for Scale
If inputs are already sorted, a heap-based k-way merge avoids sorting all elements from scratch.
This keeps only one frontier element per list in the heap, which scales well when there are many large lists.
Streaming Output Instead of Building Full Result
If consumers can process values incrementally, yield values as a stream.
Streaming is useful when merged output is large or consumed by another iterator pipeline.
Preserve Determinism with Ties
Equal values from different lists should still produce deterministic order for reproducible tests. Including list index and element index in heap tuples gives stable tie-breaking.
That is why tuple fields are typically (value, list_index, element_index) and not only value.
Benchmark with Representative Input
Do not assume one strategy is always fastest. Python sorted is highly optimized in native code and can outperform heap merge for small data sizes.
A minimal benchmark pattern:
Measure against your actual workload size and list count.
Production Style Versus Golf Style
For code golf posts, shortest valid expression is the goal. For production code, readability and tests matter more than character count.
A practical approach:
- Keep a concise one-liner for explanation and simple scripts.
- Use named heap merge function in library code.
- Add unit tests for empty lists, duplicates, and mixed lengths.
Example tests:
Common Pitfalls
- Assuming flatten-and-sort is always the best choice at large scale.
- Forgetting empty sublists and causing index errors in heap initialization.
- Losing duplicate values during merge logic.
- Ignoring deterministic tie handling and getting unstable test output.
- Over-optimizing for character count in production paths.
Summary
- Flatten-and-sort is compact and often strong for small inputs.
- Heap-based k-way merge scales better for many pre-sorted lists.
- Generator-based merge supports streaming and lower memory use.
- Stable tie handling improves reproducibility.
- Choose strategy based on context: golf brevity or production maintainability.
Related reading
- Code Golf Gray Code
- Codility - min average slice
- Codility MinAbsSum
- Codility passing car - how to approach this problem
- ColdFusion - What's an efficient way to search an array of structs?
- Collect global top-k from each node's list in MPI
- Codility Peaks Complexity
- Codility PermCheck why my solution is not working

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.