How to Count Elements and Find Duplicates in a Python List
Count occurrences of each element in a list, extract unique values, and identify duplicates using Python dictionaries and sets.
Python code
32 linesdef analyze_counts(data):
"""Count elements, return unique values, and find duplicates."""
# Count occurrences using a dictionary
counts = {}
for item in data:
counts[item] = counts.get(item, 0) + 1
# Alternative compact approach with set
unique_items = set(data)
# Find duplicates: items that appear more than once
duplicates = {item for item, count in counts.items() if count > 1}
return {
"counts": counts,
"unique_items": unique_items,
"duplicates": duplicates,
"total_items": len(data),
"unique_count": len(unique_items)
}
if __name__ == "__main__":
sample_data = ["apple", "banana", "apple", "orange", "banana", "apple"]
result = analyze_counts(sample_data)
print("Counts:", result["counts"])
print("Unique items:", result["unique_items"])
print("Duplicates:", result["duplicates"])
print("Total items:", result["total_items"])
print("Unique count:", result["unique_count"])
Output
Counts: {'apple': 3, 'banana': 2, 'orange': 1}
Unique items: {'orange', 'banana', 'apple'}
Duplicates: {'banana', 'apple'}
Total items: 6
Unique count: 3
How it works
The counts.get(item, 0) call safely fetches the current count or defaults to 0, avoiding a KeyError on first occurrence. The dictionary stores each unique element as a key and its frequency as the value. Converting the original list to a set (set(data)) automatically removes duplicates because sets only hold unique items. A set comprehension filters the counts dictionary to find items whose count exceeds 1, revealing duplicates in one line. This combined approach gives you counts, uniqueness, and duplicates in a single pass through the data.
Common mistakes
- Using `counts[item]` without initializing missing keys — raises KeyError
- Assuming sets preserve the original list order — output order is arbitrary
- Counting with lists instead of dictionaries, which is O(n²) instead of O(n)
Variations
- Use `collections.Counter(data)` for a built-in counting dictionary with .most_common()
- Use `list(dict.fromkeys(data))` to preserve insertion order when extracting unique items
Real-world use cases
- Analyzing user interaction logs to find which features are used most and duplicate click patterns.
- Building an inventory system to count remaining stock per SKU and flaging duplicate part numbers.
- Cleaning a CSV export by identifying duplicate customer IDs before loading into a CRM.
Sponsored
More from Dictionaries & sets
- Build a Case-Insensitive Dict with a Wrapper Class in Python medium
- Build a defaultdict histogram of categories in Python easy
- Build adjacency dict graph from edges in Python easy
- Build an OrderedDict insertion order demo in Python 3 easy
- Check Invertible Mapping for Duplicate Values in Python easy
- Compare Two Dictionaries in Python easy
Keep learning
Related tutorials and quizzes for this topic.