Get unique values from a List in Python

In this article, we will discuss different ways to fetch unique values from a list in Python.

Table Of Contents

Introduction

Suppose we have a list of numbers i.e.

[12, 48, 23, 48, 54, 12, 71, 48, 71, 48, 23, 48]

This list has certain repeated elements. But we want to find out all unique value in this list. Like,

[12, 48, 23, 54, 71]

There are different ways to get unique values from a list. Let’s discuss them one by one,

Advertisements

Get unique values in list using for loop

Create a new empty list X. Then iterate over all elements in the given list and for each element check if it exists in the new list X or not. If not then add that element to list X. When for loop ends, the list X will have all unique values from the given list.

For example,

listOfNums = [12, 48, 23, 48, 54, 12, 71, 48, 71, 48, 23, 48]

uniqueElements = []

for elem in listOfNums:
    if elem not in uniqueElements:
        uniqueElements.append(elem)

print(uniqueElements)

Output:

[12, 48, 23, 54, 71]

We fetched all unique values from a list in Python.

Get unique values in list using set

A set in python can have unique values only. So, to find the unique values from a list, we can create a set from the list. This set will have unique values only. Then we can cast this set to a new list.

For example,

listOfNums = [12, 48, 23, 48, 54, 12, 71, 48, 71, 48, 23, 48]

# Get unique values from a list
uniqueElements = list(set(listOfNums))

print(uniqueElements)

Output:

[71, 12, 48, 54, 23]

Get unique values in list using NumPy

Convert list to a numpy array and then pass that to numpy.unique() function. It will return an array of unique values only. Then cast it back to a list. This list will have unique values only.

For example,

import numpy as np

listOfNums = [12, 48, 23, 48, 54, 12, 71, 48, 71, 48, 23, 48]

# Get unique values from a list
uniqueElements = list(np.unique(np.array(listOfNums)))

print(uniqueElements)

Output:

[12, 23, 48, 54, 71]

Get unique values in list using Counter

Create a Counter object from the list. This counter object, will have a mapping of unique values from list and thier occurrence count. We can cast this counter object to a list. This list will have unique values only from the original list.

For example,

from collections import Counter

listOfNums = [12, 48, 23, 48, 54, 12, 71, 48, 71, 48, 23, 48]

# Get unique values from a list
uniqueElements = list(Counter(listOfNums))

print(uniqueElements)

Output:

[12, 23, 48, 54, 71]

Summary

We learned about different ways to get unique elements from a list in Python. Happy Coding.

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.

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

Scroll to Top