Check if all elements in a List are unique in Python

This tutorial will discuss about unique ways to check if all elements in a list are unique in Python.

Table Of Contents

Method 1: Using Set

In this solution we are going to use the Set Data Structure of Python. A Set in Python contains only unique elements. So, we can initialise a Set with the list contents, and after that we can compare the size of Set and List. If both set and list have equal number of elements, then it means that the given list has only unique elements. We have created a function for this,

def all_unique(listObj):
    '''Returns True if list has no duplicates'''
    return len(listObj) == len(set(listObj))

It accepts a list as an argument, and returns True, if given list has no duplicates i.e. if all elements in List are unique. To initialise a Set with the list contents, we can just pass the list to the set() function, and it will give us a Set object initialised with the list contents. If list has any duplicate element, then it will not be added in the Set. Therefore, to confirm if all elements in list are unique, we can compare the size of list and set.

Let’s see the complete example,

def all_unique(listObj):
    '''Returns True if list has no duplicates'''
    return len(listObj) == len(set(listObj))

# Example 1
numbers = [34, 23, 56, 73, 78, 11]

if all_unique(numbers):
    print("All elements in list are unique")
else:
    print("All elements in list are not unique")


# Example 2
numbers = [34, 23, 34, 73, 34, 11]

if all_unique(numbers):
    print("All elements in list are unique")
else:
    print("All elements in list are not unique")

Output

All elements in list are unique
All elements in list are not unique

Method 2: Using Dictionary

In this solution we are going to use the Dictionary Data Structure of Python.

A Dictionary in Python can have unique keys only. So, we can iterate over all the elements of list and add them as keys in the dictionary. But before adding them, we can check if the given element already exists in dictionary as a key or not. If yes, then it means that it is a duplicate element. We have created a function to check if all elements in list are unique or not.

def only_unique(listObj):
    '''Returns True if all elements in list are unique'''
    result = True
    # Create a dictionary
    dictObj = {}
    # Loope over list elements
    for elem in listObj:
        # check if list element exist in dict as key
        if elem in dictObj:
            result = False
            break
        else:
            # Add list element in the dictionary as key
            dictObj[elem] = True
    return result

It accepts a list as an argument, and returns True if given list has no duplicates i.e. if all elements in List are unique.

Let’s see the complete example,

def only_unique(listObj):
    '''Returns True if all elements in list are unique'''
    result = True
    # Create a dictionary
    dictObj = {}
    # Loope over list elements
    for elem in listObj:
        # check if list element exist in dict as key
        if elem in dictObj:
            result = False
            break
        else:
            # Add list element in the dictionary as key
            dictObj[elem] = True
    return result

# Example 1
numbers = [34, 23, 56, 73, 78, 11]

if only_unique(numbers):
    print("All elements in list are unique")
else:
    print("All elements in list are not unique")


# Example 2
numbers = [34, 23, 34, 73, 34, 11]

if only_unique(numbers):
    print("All elements in list are unique")
else:
    print("All elements in list are not unique")

Output

All elements in list are unique
All elements in list are not unique

Summary

We learned about different ways to check if a list has any duplicate element or if all elements in list are unique. Thanks.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top