Python lists allow storing non-unique values.
You can get unique values from a list in Python using a set(num_list) statement.
In this tutorial, you’ll learn how to get unique values from a list in Python using different available methods.
If You’re in Hurry…
You can use the set()
method to get unique values from a list
. Python set
can contain only unique values. While converting a list
to a set
, unique values will be added to a list
.
Code
num_list = [10, 20,30,30,40,50,50]
unique_numbers = list(set(num_list))
unique_numbers
If You Want to Understand Details, Read on…
There are different methods to get unique values from a Python list
. Let’s look into each method.
Table of Contents
Using Set() method
You can use the set() method to get unique values from a list
.
The set()
method returns a new set
with the unique objects available in the iterable(Example: List
).
Code
num_list = [10, 20,30,30,40,50,50]
unique_numbers = set(num_list)
unique_numbers
Output
{40, 10, 50, 20, 30}
If you want to have the unique items in the list
object instead of set
, you can convert the set
object into the list
again.
num_list = [10, 20,30,30,40,50,50]
unique_numbers = list(set(num_list))
unique_numbers
Output
[40, 10, 50, 20, 30]
Using numpy.unique
You can use the numpy.unique() method to get unique values from a list
.
The unique()
method returns the unique items from the array.
You need to convert a list
to a NumPy array using np.array()
method.
Then use the NumPy
array with the np.unique()
method to get the unique values.
Code
The code below demonstrates how to convert a list to a NumPy array and get unique values from it.
import numpy as np
num_list = [10, 20,30,30,40,50,50]
x = np.array(num_list)
np.unique(x)
Output
You’ll see the below output. The unique items will be available in the array.
array([10, 20, 30, 40, 50])
Using Append
The append()
method allows you to append new items to the list.
To get the unique values from the list using the append() method,
- Iterate over the list
- Check if the item is available in the new array
- If the item is NOT existing, add it to the new array. Else, do not add it.
Code
def find_unique(num_list):
unique_list = []
for x in num_list:
if x not in unique_list:
unique_list.append(x)
for x in unique_list:
print(x)
num_list = [10, 20,30,30,40,50,50]
find_unique(num_list)
Output
The above method gets the unique values from the lists and prints them.
10
20
30
40
50
Using Counter
You can also use the counter class from the python collection to get the unique values from a list.
The counter
is a dict subclass for counting hashable objects.
It counts the object and has the count of each item as a dictionary. The dictionary key consists of the item itself, and the values denote the number of times the item occurs.
To get the unique values, you can print the counter dictionary using the star * operator. The * operator displays only the keys from the dictionary. This means that only the unique values from the list are printed.
Code
The code below demonstrates how to use the counter to get the unique values from a list.
from collections import Counter
num_list = [5, 10, 5, 10, 20, 30]
print(*Counter(num_list))
Output
5 10 20 30
If you would like to know the number of occurrences of each object, you can directly use the counter object as demonstrated below.
Counter(num_list)
Output
Counter({5: 2, 10: 2, 20: 1, 30: 1})
Conclusion
You’ve learned how to get unique values from a list in Python.
Also learned the different methods available to get the unique items from a list and when it is appropriate to use them.