# Get Unique Values from a List in Python

Rate this post

[toc]

Problem Statement: How to get unique values from a list in Python?

While working with lists, quite often, there are many duplicate items present in it. There can be situations where you may need to eliminate these duplicate values and retain unique values within the list. So, how do we achieve this feat? Hence, in this article, we are going to look at the various ways to fetch unique values from the list.

Example: Suppose this is the given list:

`l= ['python', 'java', 'C', 'C', 'java', 'python', 'ruby', 'python']`

Output: The resultant list with unique values for the above list will be:

``['python', 'java', 'C', 'ruby']``

Now let’s look at the different methods to get these unique values from the list.

## Method 1: Using append() Function

Approach: You can obtain the unique values from the list using the `append` method.

• First, we need to create an empty list.
• Further, we are going to traverse the given list from the start till the end.
• Check if the value is present in the empty list.
• If it’s not present, then append the value to the empty list that we created initially.
• If it’s already present, then do not append the value.
• Finally, return this resultant list.

A quick recap to the append() method
`append` is a built-in list method in Python that is used to store/attach/include/append a value to the end of a given list. Look at the diagram given below to visualize how this method works.

Solution:

```li = ['python', 'java', 'C', 'C', 'java', 'python', 'ruby', 'python']
print("Given List: ")
print(li)
print("The unique values from the list:")
l = []
for ele in li:
if ele not in l:
l.append(ele)
print(l)```

Output:

``````Given List:
['python', 'java', 'C', 'C', 'java', 'python', 'ruby', 'python']
The unique values from the list:
['python', 'java', 'C', 'ruby']``````

Discussion: Since you have to create an extra list to store the unique values, this approach is not the most efficient approach to find unique values as it takes a lot of time and space.

## Method 2: Using set()

Another effective approach to use the `set()` method. Set is a built-in data type that does not contain any duplicate elements.

Read more about sets here – “The Ultimate Guide to Python Sets

Approach: First, we need to pass the values of the list into a set and then convert the set into a list to print the list with only unique values.

Solution:

```# Given list
l= ['python', 'java', 'C', 'C', 'java', 'python', 'ruby', 'python']
print("The unique values from the list:")
# Using set to get unique values
s = set(l)
# Converting set to list
l2 = list(s)
print(l2)```

Output:

``````The unique values from the list:
['ruby', 'python', 'C', 'java']``````

## Method 3: Using Dictionary fromkeys()

Python dictionaries have a method known as `fromkeys()` that is used to return a new dictionary from the given iterable ( such as list, set, string, tuple) as keys and with the specified value. If the value is not specified by default, it will be considered as None. Here’s a quick example:

```my_dictionary = (['key1', 'key2', 'key3'])
thisdict = dict.fromkeys(my_dictionary)
print(thisdict)

# {'key1': None, 'key2': None, 'key3': None}```

Approach: Well! We all know that keys in a dictionary must be unique. Thus, we will pass the list to the `fromkeys()` method and then use only the key values of this dictionary to get the unique values from the list.

Solution:

```# Given list
l = ['python', 'java', 'C', 'C', 'java', 'python', 'ruby', 'python']
# Using dictionary fromkeys()
# list elements get converted to dictionary keys. Keys are always unique!
x = dict.fromkeys(l)
# storing the keys of the dictionary in a list
l2 = list(x.keys())
print("The unique values from the list:")
print(l2)```

Output:

``````The unique values from the list:
['python', 'java', 'C', 'ruby']``````

## Method 4: Using Counter

We can use the `counter` function from the collections library to print all the unique elements. The `counter` function creates a dictionary where the keys are the unique elements and the values are the count of that element.

Solution:

```# Importing Counter from collections library
from collections import Counter

# Given list
l = ['python', 'java', 'C', 'C', 'java', 'python', 'ruby', 'python']
# Using Counter
c = Counter(l)
print(c)
# Only printing the keys from the dictionary
print("The unique values from the list:")
l2 = []
for key in c:
l2.append(key)
print(l2)```

Output:

``````Counter({'python': 3, 'java': 2, 'C': 2, 'ruby': 1})
The unique values from the list:
['python', 'java', 'C', 'ruby']``````

We can also directly print all the keys of the dictionary using the “*” symbol.

Example:

```# Importing Counter from collections library
from collections import Counter
# Given list
l= ['python', 'java', 'C', 'C', 'java', 'python', 'ruby', 'python']
# Using Counter
c = Counter(l)
# Only printing the keys from the dictionary
print("The unique values from the list:")
print(*c)```

Output:

``````The unique values from the list:
python java C ruby
``````

## Method 5: Using Numpy Module

We can use Python’s Numpy module to get the unique values from the list. Import the NumPy module to use the `NumPy.unique()` function that returns the unique values from the list.

Example:

```# Importing the numpy module
import numpy as np
# Given list
l = ['python', 'java', 'C', 'C', 'java', 'python', 'ruby', 'python']
# Using unique() function from numpy module
l2 = []
for ele in np.unique(l):
l2.append(ele)
print("The unique values from the list:")
print(l2)```

Output:

``````The unique values from the list:
['C', 'java', 'python', 'ruby']``````

## Method 6: Using List Comprehension

We can use a list comprehension to declare the elements in the list without using any loop. We will use the list comprehension along with the `dict()` and `zip()` function to create a counter for the unique elements. Then we will simply print the keys of the dictionary.

What is the zip() function in Python? The `zip()` function in Python is used to return an object which is generally the iterator of the tuples, where the first item in each passed iterator is paired together, then the second items are paired together and so on. If these iterators have different lengths, the iterator that has the lowest amount of items will decide the length of the new iterator.
Syntax: `zip(iterator1, iterator2, iterator3 ...)`

Example:

```# Given list
l = ['python', 'java', 'C', 'C', 'java', 'python', 'ruby', 'python']
# List comprehension using zip()
l2 = dict(zip(l, [l.count(i) for i in l]))
print("The unique values from the list:")
print(list(l2.keys()))```

Output:

``````The unique values from the list:
['python', 'java', 'C', 'ruby']``````

## Conclusion

To sum things up, there are numerous ways of generating unique elements from a list. Please feel free to use any of the above-mentioned approaches to solve your problem. If you found this article helpful and want to receive more interesting solutions and discussions in the future, please subscribe and stay tuned!