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.

Easy Python 3.9+ Aug 9, 2026 Dictionaries & sets 12 views 0 copies

Python code

32 lines
Python 3.9+
def 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

stdout
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

  1. Use `collections.Counter(data)` for a built-in counting dictionary with .most_common()
  2. 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

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Dictionaries & sets

Related tutorials and quizzes for this topic.