This post will discuss how to find duplicate items in a list in Python.

1. Using index() function

A simple solution is to get iterate through the list with indices using list comprehension and check for another occurrence of each encountered element using the index() function. The time complexity of this solution would be quadratic, and the code does not handle repeated elements in output.

Download  Run Code

2. Using In operator

Alternatively, you can use slicing with the in operator to search in the already visited portion of the list. The time complexity of the solution remains quadratic and allows repeated elements in the output.

Download  Run Code

3. Using Set (Efficient)

To improve performance and get the work done in linear time, you can use the set data structure.

Download  Run Code

 
To get each duplicate only once, you can use the set comprehension, as shown below:

Download  Run Code

4. Using count() function

Here’s an alternate solution using the count() function, which provides a simple, clean way to identify duplicates in a list. This is not recommended for large lists as the time complexity is quadratic.

Download  Run Code

5. Using iteration_utilities module

Finally, the iteration_utilities module offers the duplicates function, which yields duplicate elements. You can use this as:

 
To get each duplicate only once, combine it with unique_everseen():

That’s all about finding the duplicate items in a list in Python.

 
Also See:

Remove duplicate values from a Python list