Find Repeated Values in List in Python (2 Examples)

 

Hi! This tutorial will show you how to detect repeated values in a list in the Python programming language.

Here is an overview:

Let’s get into the Python code!

 

Create Demo Python List of Integer Values

Here, we will create the demo Python list of integers whose repeated values we will extract in this tutorial.

So, in your favorite Python IDE, run the line of code below to create the demo list of integers:

my_list = [1, 2, 3, 2, 4, 3, 5, 6, 5, 7]
 
print(my_list)
 
# [1, 2, 3, 2, 4, 3, 5, 6, 5, 7]
 
print(type(my_list))
 
# <class 'list'>

Now that we have created the demo list of integers, we can now explore examples of how to find repeated values in the list.
 

Example 1: Detect Repeated Values in List Using For Loop & count() Method

In this first example, we will use a for loop and the count() method to determine the repeated values in the list:

repeated_values = []
 
for item in my_list:
    if my_list.count(item) > 1:
        if item not in repeated_values:
            repeated_values.append(item)
 
print("Repeated values in the list:", repeated_values)
 
# Repeated values in the list: [2, 3, 5]

This method uses a for loop to iterate through each element in the list and the count() method to count the number of occurrences of each element in the list.

If the count is greater than 1, it means the element is repeated, so we add it to a new list called “repeated_values”. We also add an additional check to avoid adding duplicate elements to the “repeated_values” list.
 

Example 2: Detect Repeated Values in List Using set() Function & List Comprehension

In this next example, we will use the set() function and list comprehension to get the repeated values in the list:

repeated_values = [x for x in set(my_list) if my_list.count(x) > 1]
 
print("Repeated values in the list:", repeated_values)
 
# Repeated values in the list: [2, 3, 5]

This method uses a set to remove duplicates from the list, and a list comprehension to create a new list containing only the elements that appear more than once in the original list.

We first convert the list to a set to remove all duplicates, then use the count() method to check how many times each element appears in the original list. If an element appears more than once, we add it to a new list called “repeated_values”.

So, with that, we have demonstrated in this tutorial how to find the repeated values in a list in Python. I hope you found it helpful!
 

Video, Further Resources & Summary

Do you need more explanations on how to find the repeated values in a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to find the repeated values in a list in Python.

 

The YouTube video will be added soon.

 

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:

This post has shown how to find the repeated values in a list in Python. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top