• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Check If a List has Duplicate Elements

Check If a List has Duplicate Elements

Author: Aditya Raj
Last Updated: September 2, 2021

Lists are the most used data structures in Python. While programming, you may land into a situation where you will need a list containing only unique elements or you want to check if a list has duplicate elements. In this article, we will look at different ways to check if a list has duplicate elements in it.

Table of Contents
  1. Check if a list has duplicate Elements using Sets
  2. Check if a list has duplicate elements using the count() method
  3.  Check if a list has duplicate elements using the counter() method
  4. Conclusion

Check if a list has duplicate Elements using Sets

We know that sets in Python contain only unique elements. We can use this property of sets to check if a list has duplicate elements or not. 

For this, we will create a set from the elements of the list. After that, we will check the size of the list and the set. If the size of both the objects are equal, it will confirm that the list has no duplicate elements. If the size of the set is greater than the list, it will mean that the list contains duplicate elements.  We can understand this from the following example.

def check_duplicate(l):
    mySet = set(l)
    if len(mySet) == len(l):
        print("List has no duplicate elements.")
    else:
        print("The list contains duplicate elements")


list1 = [1, 2, 3, 4, 5, 6, 7]
print("List1 is:", list1)
check_duplicate(list1)
list2 = [1, 2, 1, 2, 4, 6, 7]
print("List2 is:", list2)
check_duplicate(list2)

Output:

List1 is: [1, 2, 3, 4, 5, 6, 7]
List has no duplicate elements.
List2 is: [1, 2, 1, 2, 4, 6, 7]
The list contains duplicate elements

In the above approach, we need to create a set from all the elements of the list. After that, we also check the size of the set and the list. These operations are very costly. 

Instead of using this approach, we can search only for the first duplicate element. To do this, we will start from the first element of the list and will keep adding them to the set. Before adding the elements to the set, we will check if the element is already present in the set or not. If yes, the list contains duplicate elements. If we are able to add each element of the list to the set, the list does not contain any duplicate element. This can be understood from the following example.

def check_duplicate(l):
    visited = set()
    has_duplicate = False
    for element in l:
        if element in visited:
            print("The list contains duplicate elements.")
            has_duplicate = True
            break
        else:
            visited.add(element)
    if not has_duplicate:
        print("List has no duplicate elements.")


list1 = [1, 2, 3, 4, 5, 6, 7]
print("List1 is:", list1)
check_duplicate(list1)
list2 = [1, 2, 1, 2, 4, 6, 7]
print("List2 is:", list2)
check_duplicate(list2)

Output:

List1 is: [1, 2, 3, 4, 5, 6, 7]
List has no duplicate elements.
List2 is: [1, 2, 1, 2, 4, 6, 7]
The list contains duplicate elements.

Check if a list has duplicate elements using the count() method

To check if a list has only unique elements, we can also count the occurrence of the different elements in the list. For this, we will use the count() method. The count() method, when invoked on a list, takes the element as input argument and returns the number of times the element is present in the list.  

For checking if the list contains duplicate elements, we will count the frequency of each element. At the same time, we will also maintain a list of visited elements so that we don’t have to count the occurrences of the visited elements. Once the count of any element is found to be greater than one, it will prove that the list has duplicate elements. We can implement this as follows.

def check_duplicate(l):
    visited = set()
    has_duplicate = False
    for element in l:
        if element in visited:
            pass
        elif l.count(element) == 1:
            visited.add(element)
        elif l.count(element) > 1:
            has_duplicate = True
            print("The list contains duplicate elements.")
            break
    if not has_duplicate:
        print("List has no duplicate elements.")


list1 = [1, 2, 3, 4, 5, 6, 7, 8]
print("List1 is:", list1)
check_duplicate(list1)
list2 = [1, 2, 1, 2, 4, 6, 7, 8]
print("List2 is:", list2)
check_duplicate(list2)

Output:

List1 is: [1, 2, 3, 4, 5, 6, 7, 8]
List has no duplicate elements.
List2 is: [1, 2, 1, 2, 4, 6, 7, 8]
The list contains duplicate elements.

 Check if a list has duplicate elements using the counter() method

We can also use the counter() method to check if a list has only unique elements or not. The counter() method. The counter() method takes an iterable object as an input and returns a python dictionary in which the keys consist of the elements of the iterable object and the values associated with the keys are the frequency of the elements. After getting the frequency of each element of the list using the counter() method, we can check if the frequency of any element is greater than one or not. If yes, the list contains duplicate elements. Otherwise not.

from collections import Counter


def check_duplicate(l):
    counter = Counter(l)
    has_duplicate = False
    frequencies = counter.values()
    for i in frequencies:
        if i > 1:
            has_duplicate = True
            print("The list contains duplicate elements.")
            break
    if not has_duplicate:
        print("List has no duplicate elements.")


list1 = [1, 2, 3, 4, 5, 6, 7, 8]
print("List1 is:", list1)
check_duplicate(list1)
list2 = [1, 2, 1, 2, 4, 6, 7, 8]
print("List2 is:", list2)
check_duplicate(list2)

Output:

List1 is: [1, 2, 3, 4, 5, 6, 7, 8]
List has no duplicate elements.
List2 is: [1, 2, 1, 2, 4, 6, 7, 8]
The list contains duplicate elements.

Conclusion

In this article, we have discussed four ways to check if a list has only unique elements or not. We have used sets, count() and counter() methods to implement our approaches. To learn more about lists, you can read this article on list comprehension.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us