Only Add Unique Item To 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
If you want to add an item to a Python list only when it is not already present, the simplest solution is a membership check before append. That works well for small lists, but once the collection grows or uniqueness becomes a core requirement, it is worth choosing a data structure that matches the rule more directly.
The Simple List Check
For many scripts, this is enough:
This preserves list order and keeps the code easy to read. The tradeoff is performance. candidate not in items scans the list linearly, so each check is O(n).
That is perfectly acceptable when:
- the list is small
- inserts are infrequent
- readability matters more than optimization
Use a Set When Fast Uniqueness Matters
If uniqueness checks happen often, a set is usually the better primary structure because membership is average-case O(1).
The downside is that sets are unordered in the general conceptual sense, even though current Python implementations preserve insertion order as an implementation detail for dictionaries, not as a list replacement contract for all use cases. If presentation order matters, use a combined pattern.
Preserve Order with a List and a Set
One common approach is to store ordered values in a list and store membership in a companion set.
This gives you:
- fast uniqueness checks
- stable insertion order
- explicit control over output
It is a strong default when you are building menus, tags, or deduplicated event streams.
Handling Unhashable Items
Sets only work with hashable values. Lists, dictionaries, and other mutable objects cannot be added directly to a set. In that case, derive a stable key from the item.
Here, uniqueness is based on id, not on the entire dictionary object. That is usually what you actually mean in application code.
Wrap the Pattern in a Helper
If you do this often, a helper function keeps the call site tidy:
For higher-throughput code, write a helper that manages both a list and a set together instead of repeating the bookkeeping across the codebase.
When a List Is the Wrong Structure
Sometimes the real answer is not “make the list smarter” but “stop using a list for uniqueness.” If the collection is fundamentally a set of unique values, model it as a set first and convert to a list only when you need sequence behavior.
That tends to reduce bugs because the data structure itself enforces the rule instead of relying on every caller to remember an if statement.
Common Pitfalls
The most common mistake is repeatedly using if item not in items on a large list inside a loop. It works, but performance degrades as the list grows because every membership test scans from the beginning.
Another issue is assuming a set is a drop-in replacement for a list. Sets remove duplicates quickly, but they do not support indexing and are not the right choice if your downstream code depends on sequence operations.
For object-like records, developers also often compare the whole object when they really need a business key such as id or email. That leads to accidental duplicates that look different in memory but represent the same entity.
Finally, be explicit about whether you want to keep the first occurrence or replace it with a later one. “Unique” does not answer that policy question by itself.
Summary
- For small lists,
if item not in itemsbeforeappendis fine. - For frequent checks, a
setgives much faster membership tests. - Use a list plus a set when you need both order and uniqueness.
- For unhashable records, deduplicate by a stable key.
- Pick the data structure that enforces the rule you actually need.
Related reading
- OpenCV decision tree parameters issue
- Operation on every pair of element in a list
- Operation Queue vs Dispatch Queue for iOS Application
- Optimal algorithm for returning top k values from an array of length N
- Open file in a relative location in Python
- Open files in 'rt' and 'wt' modes
- Optimal algorithm to return largest k elements from an array of infinite number of elements in running stream
- Optimal bubble sorting algorithm for an array of arrays of numbers

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.