Find Duplicate Elements in a Python List

Identifies and returns duplicate elements from a Python list using sets for efficient membership tests.

Easy Python 3.9+ Aug 9, 2026 Lists & loops 13 views 0 copies

Python code

13 lines
Python 3.9+
def find_duplicates(lst):
    seen = set()
    duplicates = set()
    for item in lst:
        if item in seen:
            duplicates.add(item)
        else:
            seen.add(item)
    return list(duplicates)

if __name__ == "__main__":
    sample = [1, 2, 3, 2, 4, 1, 5, 3]
    print(find_duplicates(sample))

Output

stdout
[1, 3, 2]

How it works

This function uses two sets: seen tracks items already encountered, and duplicates stores items that appear more than once. For each element, if it's already in seen, it's added to duplicates; otherwise, it's added to seen. Since sets ignore ordering, the output order may vary between runs. The solution runs in O(n) time on average.

Common mistakes

  • Using a list to track seen items, which makes the function O(n^2).
  • Not converting the result to a list, leading to an unordered set output.
  • Counting occurrences and then filtering, which is less efficient for large lists.

Variations

  1. Use a dictionary to count occurrences and then filter keys with count > 1.
  2. Use a list comprehension with counting but note it is slower for large data.

Real-world use cases

  • Detecting duplicate user IDs in a dataset before syncing to a CRM.
  • Finding repeated error codes in a log file to highlight frequent issues.
  • Identifying duplicate SKU entries in an inventory CSV before import.

Sponsored

Run this sample

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

Open editor

More from Lists & loops

Related tutorials and quizzes for this topic.