Find unique elements in python list

Milind Soorya / September 21, 2021

1 min read

find number of unique elements in list python

What is python set() method

Using set() property of Python, we can easily check for the unique values. Insert the values of the list in a set. Set only stores a value once even if it is inserted more then once. After inserting all the values in the set by list_set=set(list1), convert this set to a list to print it.

# Python program to check if two
# to get unique values from list
# using set

# function to get unique values
def unique(list1):

    # insert the list to the set
    list_set = set(list1)
    # convert the set to the list
    unique_list = (list(list_set))
    for x in unique_list:
        print x,

# driver code
list1 = [10, 20, 10, 30, 40, 40]
print("the unique values from 1st list is")
# the unique values from 1st list is
# 40 10 20 30

list2 =[1, 2, 1, 1, 3, 4, 3, 3, 5]
print("\nthe unique values from 2nd list is")
# the unique values from 1st list is
# 1 2 3 4 5


Learn about building products as a Data Scientist

Get a once-per-month email with my latest article and additional details about my launches, products, and experiments ✨

No spam, sales, or ads. Unsubscribe as your heart desires.