Python lists allow storing non-unique values.
You can get unique values from a list in Python using a set(num_list) statement.
Basic Example
num_list = [10, 20,30,30,40,50,50]
unique_numbers = list(set(num_list))
unique_numbers
In this tutorial, you’ll learn how to get unique values from a list in Python using different available methods.
Table of Contents
Using Set() method
The set()
method returns a new set
with the unique objects available in the iterable(Example: List
).
- Use the set() method to get unique values from a
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
,
- Convert the
set
object into thelist
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
The unique()
method returns the unique items from the array.
- Convert a
list
to a NumPy array usingnp.array()
method - Use the
NumPy
array with thenp.unique()
method to get the unique values
Code
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 appends new items to the existing 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
10
20
30
40
50
Using Counter
The counter
is a dict subclass for counting hashable objects.
You can also use the counter class from the python collection to get the unique values from a list.
- It counts the object and has the count of each item as a dictionary.
- The dictionary key consists of the items in the list, 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
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, directly use the counter object as demonstrated below.
Counter(num_list)
Output
Counter({5: 2, 10: 2, 20: 1, 30: 1})
Additional Resources
- How To Concatenate Lists in Python
- How to check if a list is empty in Python
- How To Check If A Value Exists In A List In Python (Speed Compared)
- How to Remove an element from a List Using Index in Python
- How To Get The Last Element Of A List In Python
- How To Remove Duplicates From the List In Python