5 ways to get unique value from a list in python

In this post, we will go through the 5 ways to get unique value from a list in python. We will learn these methods with the help of examples. During our programming, many times we need to sort or search elements from a huge collection of data. In this article, we will cover one of such use cases where we need to find the unique element value from a list.

There are many techniques and methods that developers can use to do this job. Here we will covering 5 different methods to search the elements of unique value.

The methods which we are going to use are:

  • The simple way of using the loop.
  • Using Sets
  • Using NumPy
  • Using Dictionary
  • Using OrderedDict

So lets begin to learn the methods one by one by using examples:

1. Simple way using loop

The first approach is by using a loop. So let us see how the below code is doing the job for us. We have a list of elements with the language names.

As you can see we have some elements that are repetitive. So our target is to eliminate the duplicate items from this list and get us a list with unique values. We will be using the loop to check values one by one.

LangList = ['C#', 'python', 'C', 'Java','C', 'Go','C#','Go','C']
uniqueList =[]
for element in LangList:
    if element not in uniqueList:

Output : As you can see from the below output now we have a list with all unique values.

['C#', 'python', 'C', 'Java', 'Go']

2. Using Set

The second approach is by using the set. In this example we will use the list(LangList) and pass it to set().It returns list of unique elements.


LangList = ['C#', 'python', 'C', 'Java','C', 'Go','C#','Go','C']

langlstset = set(LangList) 
    # parse the set to list 
uniquelist = (list(langlstset)) 
print ("The unique list:",uniquelist)

Output: Here also you can see that the resulting list have all unique values.

The unique list: ['Java', 'python', 'C#', 'Go', 'C']

3. Using NumPy

NumPy is a Python library that is very useful when dealing with arrays and multi-dimension data. In this example, we will make use of this library and its functions.

NumPy is a very powerful library and very efficient and fast. We will be using the array function of this library.

import numpy 

langlist = ['C#', 'C#', 'JAVA', 'GO', 'JAVA', 'C','Python','C','GO']   
# pass langlist to the array 

langlist = numpy.array(langlist) 
print ("the unique list:",numpy.unique(langlist))

Output : Here also we got the desired output with a list of unique values.

the unique list: ['C' 'C#' 'GO' 'JAVA' 'Python']

4. Using Dictionary

The next approach in the list is by using a Dictionary. We will be starting with the same list of elements with the names of the languages. Then we will use the dict function of the dictionary. Here is the example below:

langlist = ['C#', 'C#', 'JAVA', 'GO', 'JAVA', 'C','Python','C','GO'] 
dict = {}
for ele in langlist:
    dict[ele] = True
uniqueList = list(dict.keys())
print ("the unique list:",uniqueList)

Output: We have the desired output.

the unique list =  ['C#', 'JAVA', 'GO', 'C', 'Python']

5. Using OrderedDict()

The last approach is by using the ordered dictionary. In this approach also we will do the same job by using ordered dictionary.

from collections import OrderedDict
langlist = ['C#', 'C#', 'JAVA', 'GO', 'JAVA', 'C','Python','C','GO'] 

uniqueList = list(OrderedDict( (x,1) for x in langlist ).keys())

print ("the unique list:",uniqueList)


the unique list: ['C#', 'JAVA', 'GO', 'C', 'Python']

So we just learned 5 different techniques to find the unique elements in a list. These are not the only methods that we have available. There may be many more techniques. Our purpose was to teach you and make you aware of the concept of Searching and identifying the unique element.

We hope you learnt the concept!! Happy learning!