Problem
To get only the unique values in a Python list.
Solution
In this post, we’ll introduce three different techniques to identify if we have any repeated values in the list and obtain a list of unique values.
Option 1: Using NumPy
NumPy is a Python library that can be used to work with arrays. We can utilize the help of this package to get unique values from a list. Let us look at an example.
Code:
import numpy as np
def getuniquelist(values):
assignlist = np.array(values)
uniquelist = np.unique(assignlist)
return list(uniquelist)
repeatedlist = [1,2,3,4,4,5,3,2,1]
print("Provided list:", repeatedlist)
print("Newlist: ", getuniquelist(repeatedlist))
Output:
Provided list: [1, 2, 3, 4, 4, 5, 3, 2, 1]
Newlist: [1, 2, 3, 4, 5]
Note:
- Before importing the NumPy package, we need to install NumPy using pip install numpy in the terminal.
- Using np.array() will assign the list values to the NumPy array. By using np.unique() we can retrieve the unique list.
Option 2: Using Traversal Technique
We can traverse through the list and store each value in a new list using an if condition check to confirm we don’t have any repeated values.
Code:
def getuniquelist(values):
newlist = []
for x in values:
if x not in newlist:
newlist.append(x)
return newlist
repeatedlist = [8,8,7,9,2,2,0,7]
print("Provided list:", repeatedlist)
print("Newlist: ", getuniquelist(repeatedlist))
Output:
Provided list: [8, 8, 7, 9, 2, 2, 0, 7]
Newlist: [8, 7, 9, 2, 0]
Option 3: Using Python sets
The set() function, by default, stores a value once even if it is added more than once. So by storing all the values from the list to set, we can achieve unique occurrences.
Code:
def getuniquelist(value):
assignset = set(value)
uniquelist = list(assignset)
return uniquelist
repeatedlist = ["vinod", "greg", "john", "john", "john"]
print("Provided list:", repeatedlist)
print("Newlist: ", getuniquelist(repeatedlist))
Output:
Provided list: ['vinod', 'greg', 'john', 'john', 'john']
Newlist: ['john', 'greg', 'vinod']