How can I partition split up, divide a list based on a condition?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Partitioning a list means dividing its elements into groups according to a predicate. In Python, the most common case is a two-way split such as "items that satisfy the condition" and "items that do not," but the implementation details still matter if you care about order, memory use, or single-pass processing.
The Clearest Two-List Approach
If the list is not enormous, two list comprehensions are often the easiest solution to read.
This keeps the original order within each group and is perfectly fine for many real programs.
One Pass Is Better When Work Matters
The two-comprehension version evaluates the condition twice for every element. If the predicate is expensive or the iterable is large, a single-pass partition is more efficient.
This reads the input once and still preserves order.
Partitioning Without Losing Order
People sometimes reach for sorting or itertools.groupby, but those tools solve a different problem. groupby only groups consecutive items with the same key, so it does not automatically partition an unsorted list into all true values and all false values.
This produces alternating groups as the predicate result changes. It is useful for runs of similar items, not for a full true-versus-false split.
Partitioning Into More Than Two Buckets
If you need more than two groups, build buckets by key.
That is not just partitioning by a boolean condition anymore; it is grouping by a derived key. The idea is related, but the data structure changes from a pair of lists to a mapping of buckets.
Generator Inputs And Consumption
If the input is an iterator rather than a concrete list, remember that once you consume it, it is gone.
This works because the partition function reads the generator once and stores the results. Using two separate comprehensions over the same generator would not behave the same way because the first pass would exhaust it.
In-Place Partitioning Is A Different Problem
Sometimes people mean the in-place partition step used in quicksort, where elements are rearranged inside the same array. That is a different requirement from creating two new Python lists while preserving order. In-place partitioning can save memory, but it usually changes element order and has more implementation complexity.
For everyday Python code, returning two new lists is usually the most pragmatic answer.
Common Pitfalls
The most common mistake is using groupby and expecting it to collect all matching values across the entire list without prior sorting. Another is forgetting that two list comprehensions evaluate the condition twice and may not be suitable for expensive predicates. Developers also sometimes partition a generator twice and then wonder why the second result is empty. Finally, an in-place algorithm is not a drop-in replacement if the caller depends on stable relative order.
Summary
- Two list comprehensions are the simplest partitioning technique for ordinary lists.
- A single-pass helper is better when the predicate is expensive or the input is an iterator.
- '
itertools.groupbygroups consecutive runs, not all matching values globally.' - Grouping into many buckets is a related but different problem from boolean partitioning.
- Decide early whether you need stable order, one-pass consumption, or in-place rearrangement.

