Python function that takes a list and returns a new list with unique elements of the first list

Let us get our hands on the Python list with the more amazing problem to solve.

Method 1: Using a new list and “not in” keyword

def unique_list(lst_of_elements):
    num = []
    for a in lst_of_elements:
        if a not in num:
            num.append(a)
    
    print("The list of unique elements is: ",num)

lst_of_elements = []
n = int(input("Enter number of elements : "))
for i in range(0, n):
    ele = int(input())
    lst_of_elements.append(ele) 
print("The list entered by user : ", lst_of_elements)

unique_list(lst_of_elements)

Output:

Enter number of elements : 6
1
2
2
3
3
5
The list entered by users :  [1, 2, 2, 3, 3, 5]
The list of unique elements is:  [1, 2, 3, 5]

Explanation:

We initially took the number of elements the user is going to enter into the list. We then appended these elements to the list. This list lst_of_elements is then passed on to a unique_list function. Inside this function, we declared a new empty list named num. Now run a for loop, that will add all those elements in the num list that are not there. In order to check this, we used the Python keyword named not in.

Method 2: Using a set

def unique_element(lst_of_elements):
    list_set = set(lst_of_elements)
    unique_list = (list(list_set))
    for x in unique_list:
        print(x, ",\b")
        
lst_of_elements = []
n = int(input("Enter number of elements : "))
for i in range(0, n):
    ele = int(input())
    lst_of_elements.append(ele) 
print("The list entered by user : ", lst_of_elements)

print("the unique values from the entered list is")
unique_element(lst_of_elements)

Explanation:

Here we converted the list to the set. The set() function in Python takes the unique element only. Now that we have converted the list to a set and finally we have all the unique elements, we again convert the set to a list and run a for loop to print all those elements.

Method 3: Using the numpy library

import numpy

def unique_element(lst_of_elements):
    x = numpy.array(lst_of_elements)
    print(numpy.unique(x))
        
lst_of_elements = []
n = int(input("Enter number of elements : "))
for i in range(0, n):
    ele = int(input())
    lst_of_elements.append(ele) 
print("The list entered by user : ", lst_of_elements)

print("the unique values from the entered list is")
unique_element(lst_of_elements)

Explanation:

Here we simply used the unique() function of the NumPy library. This is one of the simplest methods that we can use.

Method 4: Using the counter()

from collections import Counter
def unique_element(lst_of_elements):
    print(*Counter(lst_of_elements))
        
lst_of_elements = []
n = int(input("Enter number of elements : "))
for i in range(0, n):
    ele = int(input())
    lst_of_elements.append(ele) 
print("The list entered by user : ", lst_of_elements)

print("the unique values from the entered list is")
unique_element(lst_of_elements)

Explanation:

We imported a library named collections and use the counter(). Python’s Counter subclass of dict is created specifically for counting hashable items. It is a dictionary where numbers are the values and objects are the keys. You normally pass a series or iterable of hashable objects as an input to the class’ function object when using Counter.

Method 5: Using the reduce() and lambda

from functools import reduce

def unique_element(lst_of_elements):
    ans = reduce(lambda re, x: re+[x] if x not in re else re, lst_of_elements, [])
    print(ans)

lst_of_elements = []
n = int(input("Enter number of elements : "))
for i in range(0, n):
    ele = int(input())
    lst_of_elements.append(ele) 
print("The list entered by user : ", lst_of_elements)

print("the unique values from the entered list is")
unique_element(lst_of_elements)

Explanation:

reduce() functools library is one of the ways we can solve this problem. We used a lambda function. re is the name of our function. x is the argument and re+[x] is our expression. We check for an element, if not in re then we add it in re. Then finally reduce it.

We saw overall 5 methods to solve the above-stated problem. Think about yours and comment here.

Share:

Author: Dhvanil V