Get unique values from a List in Python

In this article, we will discuss how to get the unique elements from a list in Python.

Table Of Contents

Get unique values from a List using for-loop

Create an empty list, which will contain the unique elements only, therefor we can call it “unique list”. Then, iterate over all the elements of original list, and for each element check if it exists in the unique list or not. If yes, then just skip it. Whereas, if it does not exist in the unique list, then add that element to the unique list. In the end, this unique list will have all the unique elements from the original list.

listOfNumbers = [67, 78, 55, 44, 78, 98, 55, 67]

uniqueValues = []

# Iterate over all elements of list
for elem in listOfNumbers:
    # Check if eleemnt already exist in unique list
    if elem not in uniqueValues:
        # If not then add element to unique list
        uniqueValues.append(elem)

print(uniqueValues)

Output:

[67, 78, 55, 44, 98]

The uniqueValues contains the unique elements of listOfNumbers.

Advertisements

Get unique values from a List using Set

A Set in Python is a kind of data structure that contains only unique values. So, if we initialize a Set from a List, then the Set will have only unique values from the list. Then we can convert that set to a list again, and this new list will have unique values only. For example,

listOfNumbers = [67, 78, 55, 44, 78, 98, 55, 67]

# get unique values from list listOfNumbers
uniqueValues = [*set(listOfNumbers)]

print(uniqueValues)

Output:

[67, 78, 55, 44, 98]

The uniqueValues contains the unique elements of listOfNumbers.

Get unique values from a List using NumPy

The NumPy module provides a function unique(). It accepts a sequence as an argument and returns a NumPy Array containing the unique values from the given sequence. So, we can pass a list to the numpy.unique() function as an argument, and it will return an array containing unique items from the list. Then we can convert that array to a list again, and this new list will have unique values only. For example,

import numpy as np

listOfNumbers = [67, 78, 55, 44, 78, 98, 55, 67]

# get unique values from list listOfNumbers
uniqueValues = np.unique(listOfNumbers).tolist()

print(uniqueValues)

Output:

[67, 78, 55, 44, 98]

The uniqueValues contains the unique elements of listOfNumbers.

Get unique values from a List using Counter

The collections module in Python has a Counter class. It is a dictionary subclass and it accepts a sequence/list as an argument in its constructor. It keeps the occurrence count of each element of sequence as a hash table. Therefore, once Counter object is initialized with a list, we can get the occurrence count of any element of list from the Counter object. All the keys in this Counter object will be the unique values of list. For example,

from collections import Counter

listOfNumbers = [67, 78, 55, 44, 78, 98, 55, 67]

# get unique values from list listOfNumbers
uniqueValues = [*Counter(listOfNumbers)]

print(uniqueValues)

Output:

[67, 78, 55, 44, 98]

The uniqueValues contains the unique elements of listOfNumbers.

Summary

We learned about four different ways to get the unique values from a list in Python. Thanks.

Pandas Tutorials -Learn Data Analysis with Python

   

Are you looking to make a career in Data Science with Python?

Data Science is the future, and the future is here now. Data Scientists are now the most sought-after professionals today. To become a good Data Scientist or to make a career switch in Data Science one must possess the right skill set. We have curated a list of Best Professional Certificate in Data Science with Python. These courses will teach you the programming tools for Data Science like Pandas, NumPy, Matplotlib, Seaborn and how to use these libraries to implement Machine learning models.

Checkout the Detailed Review of Best Professional Certificate in Data Science with Python.

Remember, Data Science requires a lot of patience, persistence, and practice. So, start learning today.

Join a LinkedIn Community of Python Developers

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