Replace list of list with condensed list of list while maintaining order
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When people want to "condense" a list of lists while maintaining order, the real question is what kind of condensation they mean. The most common interpretation is to remove repeated values while keeping the first-seen order, either within each sublist or across the whole nested structure. In Python, that is straightforward once the rule is made explicit.
Condensing Values While Preserving First Appearance
Suppose you have nested lists with repeated values:
If the goal is to produce a condensed nested result where each value appears only once overall, in the order it is first seen, iterate left to right and keep a seen set.
Output:
This keeps the original encounter order across the whole structure and removes repeated values cleanly.
Condensing Only Within Each Sublist
Sometimes the intent is narrower: remove duplicates inside each sublist but keep repeats across different rows.
Output:
dict.fromkeys is useful here because dictionaries preserve insertion order in modern Python.
Flattening Then Regrouping
A more aggressive version of condensation is to flatten all values, remove duplicates globally, and then put the result into one list.
Output:
This is appropriate when the outer grouping is no longer important and you only care about ordered unique values.
Why Order Preservation Needs Extra Care
A plain set removes duplicates, but it does not preserve the original first-seen sequence in a way you should rely on for ordered output. That is why most correct solutions combine:
- a
seenset for fast membership checks - a list for the final ordered result
This gives both good performance and predictable ordering.
For large data, the pattern stays efficient because each membership test is constant time on average.
Designing the Right Condensation Rule
Before coding, decide which of these you actually need:
- dedupe within each row only
- dedupe across all rows while preserving first appearance
- flatten everything into one condensed row
The title of the problem often sounds like one thing, but the required output may reflect another. The algorithm is only correct once the condensation rule is fixed.
Example With Numbers and Stable Grouping
Here is a version that preserves the outer structure but removes items already emitted in earlier rows.
Output:
The seen.add(item) trick works because set.add returns None, which is falsey. It is compact, but some teams prefer the more explicit loop version for readability.
Common Pitfalls
A common mistake is using a plain set as the final result and then discovering the original order is lost.
Another issue is not defining whether duplicates should be removed within each sublist, across the whole nested structure, or both. Different interpretations produce different correct outputs.
Developers also sometimes flatten everything unintentionally when they still needed to keep the outer list structure.
Finally, clever list comprehensions can make the code harder to read than a simple loop. When order rules matter, explicit loops are often the safer choice.
Summary
- First define what "condensed" should mean for your nested list.
- Use a
seenset plus ordered output list when preserving first appearance matters. - '
dict.fromkeysis a clean option for deduping within one sublist.' - Do not rely on a raw
setfor ordered output. - Prefer the simplest algorithm that matches the exact output shape you need.

