Check if all elements in List are unique in Python

In this article, we will discuss different ways to check if all elements of a List are unique or not in Python.

Table Of Contents

Method 1: Using Set

Initialize a Set from the list elements. As Set, contains only unique elements, therefore if the size of Set and List is equal, then it means list has only unique elements. Let’s see an example,

listOfNumbers = [67, 78, 15, 14, 90, 98, 77, 76]

# Check if all elements in a list are unique
if len(set(listOfNumbers)) == len(listOfNumbers):
    print('Yes, All elements in a list are unique')
else:
    print('No, All elements in a list are not unique')

Output:

Yes, All elements in a list are unique

Method 2: Using Counter

Counter from collections module, is a subclass of dictionary in Python. When we intialize a Counter object with a sequence, it keeps the frequency count of each element of sequence in the internal dictionary. So, initialize a Counter object with list and check the frequency count of each item in the Counter object. If the frequency count of each item is 1 in the Counter object, then it means list has only unique elements. Let’s see an example,

from collections import Counter

listOfNumbers = [67, 78, 15, 14, 90, 98, 77, 76]

# Create Counter object from list
counter = Counter(listOfNumbers)

result = True
# Check if all elements in a list are unique
for num, freq in counter.items():
    if freq > 1:
        result = False
        break

# Check if all elements in a list are unique
if result:
    print('Yes, All elements in a list are unique')
else:
    print('No, All elements in a list are not unique')

Output:

Yes, All elements in a list are unique

Method 3: Using Nested for loop

For each element of list, check if it is equal to any other element of list or not using a nested for loop.

Steps are as follows,

  • Iterate over all elements of list by index position using a for loop.
  • During iteration, for element at ith index, check if it matches with any element from i+1 till the end of list. For this use a nested for loop.
  • As soon as a match is found, break the loops and mark the result as False.

Let’s see an example,

def is_unique(listOfElements):
    result = True
    # Check if all elements in a list are unique
    for x in range(len(listOfElements)):
        for y in range(x+1, len(listOfElements)):
            if listOfElements[x] == listOfElements[y]:
                result = False
                return result
    return result

listOfNumbers = [67, 78, 15, 14, 90, 98, 77, 76]


# Check if all elements in a list are unique
if is_unique(listOfNumbers):
    print('Yes, All elements in a list are unique')
else:
    print('No, All elements in a list are not unique')

Output:

Yes, All elements in a list are unique

Summary

We learned about different ways to check if all elements of a list are unique or not in Python. 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