Find non-common elements in lists
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
Finding non-common elements between two lists means identifying the values that appear in one list but not the other. Depending on the problem, you may care only about unique values, or you may need to preserve order and duplicates.
That distinction matters because the best solution changes with the requirement. Sets are fast and concise for unique values, while list-based logic is better when multiplicity or original order must survive.
Use Sets for Unique Non-Common Values
If you only care about distinct values, convert both lists to sets and compute the symmetric difference.
The symmetric-difference operator returns elements that are in either set, but not both. It is concise and efficient because membership checks on sets are fast.
This is the right tool when duplicates do not matter and output ordering is unimportant.
Preserve Order with List Comprehensions
If you want to keep the original order of appearance, use membership checks directly against the other list or against a set built from it.
This keeps the ordering from each original list. It also keeps duplicates that remain non-common, which a set-based solution would discard.
Use Counter When Duplicate Counts Matter
Sometimes the requirement is closer to multiset difference. In that case, duplicate counts matter, not just membership.
Here, one copy of 2 is shared, but the extra copy on the left remains in the result. That is a different question from plain symmetric difference, and it is worth clarifying before you choose an implementation.
Choose the Definition First
The phrase "non-common elements" is ambiguous unless you define the expected behavior. Ask these questions first:
- should duplicates be preserved
- should original order be preserved
- do you want one combined result or one result per input list
For many bugs, the problem is not the code. The problem is that two people are solving different definitions of "non-common."
Time Complexity Notes
A naive list-against-list membership check can degrade to O(n * m). Converting one or both inputs to sets reduces membership checks to average O(1), which is a large improvement on bigger collections.
That is why a common hybrid pattern is:
- convert the opposite list to a set for membership tests
- use a list comprehension to preserve order in the output
This often gives you the best balance of clarity and performance.
Common Pitfalls
The biggest mistake is using sets when the caller actually needs duplicates or ordering preserved. Sets remove both.
Another common problem is writing nested loops for large lists when a set or Counter would be much cheaper.
It is also easy to return a single merged result when the caller really wants "left only" and "right only" separately.
Finally, make sure the list elements are hashable before choosing a set-based solution. Lists of dictionaries, for example, need a different approach.
Summary
- Use set symmetric difference for unique non-common values.
- Use list comprehensions with set lookups when order should be preserved.
- Use
Counterwhen duplicate counts matter. - Clarify the meaning of "non-common" before choosing the algorithm.
- Prefer set-backed membership checks over nested loops for larger inputs.
Related reading
- Find number of bits to be flipped to get maximum 1's in array
- Find number of permutations of a given sequence of integers which yield the same binary search tree
- Find object in list that has attribute equal to some value that meets any condition
- Find only two numbers in array that evenly divide each other
- Find out how much memory is being used by an object in Python
- Find p-value significance in scikit-learn LinearRegression
- Find out which combinations of numbers in a set add up to a given total
- find pair of numbers in array that add to given sum

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.