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.
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:
Frequently Asked:
[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.