Python | Get unique values from a list
Given a list, print all the unique numbers in any order.
Examples:
Input : 10 20 10 30 40 40
Output : 10 20 30 40Input : 1 2 1 1 3 4 3 3 5
Output : 1 2 3 4 5
Method 1: Traversal of the list
Using traversal, we can traverse for every element in the list and check if the element is in the unique_list already if it is not over there, then we can append it to the unique_list. This is done using one for loop and another if statement which checks if the value is in the unique list or not which is equivalent to another for a loop.
Python
# Python program to check if two # to get unique values from list # using traversal # function to get unique values def unique(list1): # initialize a null list unique_list = [] # traverse for all elements for x in list1: # check if exists in unique_list or not if x not in unique_list: unique_list.append(x) # print list for x in unique_list: print x, # driver code list1 = [ 10 , 20 , 10 , 30 , 40 , 40 ] print ( "the unique values from 1st list is" ) unique(list1) list2 = [ 1 , 2 , 1 , 1 , 3 , 4 , 3 , 3 , 5 ] print ( "\nthe unique values from 2nd list is" ) unique(list2) |
the unique values from 1st list is 10 20 30 40 the unique values from 2nd list is 1 2 3 4 5
Time Complexity: O(n*n)
Auxiliary Space: O(n)
Method 2: Using Set
Using set() property of Python, we can easily check for the unique values. Insert the values of the list in a set. Set only stores a value once even if it is inserted more than once. After inserting all the values in the set by list_set=set(list1), convert this set to a list to print it.
Python
# Python program to check if two # to get unique values from list # using set # function to get unique values def unique(list1): # insert the list to the set list_set = set (list1) # convert the set to the list unique_list = ( list (list_set)) for x in unique_list: print x, # driver code list1 = [ 10 , 20 , 10 , 30 , 40 , 40 ] print ( "the unique values from 1st list is" ) unique(list1) list2 = [ 1 , 2 , 1 , 1 , 3 , 4 , 3 , 3 , 5 ] print ( "\nthe unique values from 2nd list is" ) unique(list2) |
the unique values from 1st list is 40 10 20 30 the unique values from 2nd list is 1 2 3 4 5
Time complexity: O(n), where n is length of list.
Auxiliary Space: O(n), where n is length of list.
Method 3: Using numpy.unique
Using Python’s import numpy, the unique elements in the array are also obtained. In the first step convert the list to x=numpy.array(list) and then use numpy.unique(x) function to get the unique values from the list. numpy.unique() returns only the unique values in the list.
Python3
# Python program to check if two # to get unique values from list # using numpy.unique import numpy as np # function to get unique values def unique(list1): x = np.array(list1) print (np.unique(x)) # driver code list1 = [ 10 , 20 , 10 , 30 , 40 , 40 ] print ( "the unique values from 1st list is" ) unique(list1) list2 = [ 1 , 2 , 1 , 1 , 3 , 4 , 3 , 3 , 5 ] print ( "\nthe unique values from 2nd list is" ) unique(list2) |
Output:
the unique values from 1st list is [10 20 30 40] the unique values from 2nd list is [1 2 3 4 5]
Time complexity: O(nlogn) due to the use of the sorting algorithm used by the numpy.unique() function.
Auxiliary space: O(n) because numpy.unique() function creates a copy of the input array and then sorts it before returning the unique elements.
Method #4: Using collections.Counter()
Using python import Counter() from collections print all the keys of Counter elements or we print directly by using the “*” symbol. Below is the implementation of above approach.
Python3
# Python program to check if two # to get unique values from list # importing counter from collections from collections import Counter # Function to get unique values def unique(list1): # Print directly by using * symbol print ( * Counter(list1)) # driver code list1 = [ 10 , 20 , 10 , 30 , 40 , 40 ] print ( "the unique values from 1st list is" ) unique(list1) list2 = [ 1 , 2 , 1 , 1 , 3 , 4 , 3 , 3 , 5 ] print ( "\nthe unique values from 2nd list is" ) unique(list2) |
the unique values from 1st list is 10 20 30 40 the unique values from 2nd list is 1 2 3 4 5
Time Complexity: O(n), where n is the number of elements in the input list.
Auxiliary Space : O(n)
Method #5: Using reduce()
Using python import reduce() from functools and iterate over all element and checks if the element is a duplicate or unique value. Below is the implementation of the above approach.
Python
# Python program to check if two # to get unique values from list from functools import reduce # Function to get unique values def unique(list1): # Print directly by using * symbol ans = reduce ( lambda re, x: re + [x] if x not in re else re, list1, []) print (ans) # driver code list1 = [ 10 , 20 , 10 , 30 , 40 , 40 ] print ( "the unique values from 1st list is" ) unique(list1) list2 = [ 1 , 2 , 1 , 1 , 3 , 4 , 3 , 3 , 5 ] print ( "\nthe unique values from 2nd list is" ) unique(list2) |
the unique values from 1st list is [10, 20, 30, 40] the unique values from 2nd list is [1, 2, 3, 4, 5]
Method #6:Using Operator.countOf() method
Python3
# Python program to check if two # to get unique values from list # using traversal import operator as op # function to get unique values def unique(list1): # initialize a null list unique_list = [] # traverse for all elements for x in list1: # check if exists in unique_list or not if op.countOf(unique_list, x) = = 0 : unique_list.append(x) # print list for x in unique_list: print (x) # driver code list1 = [ 10 , 20 , 10 , 30 , 40 , 40 ] print ( "the unique values from 1st list is" ) unique(list1) list2 = [ 1 , 2 , 1 , 1 , 3 , 4 , 3 , 3 , 5 ] print ( "\nthe unique values from 2nd list is" ) unique(list2) |
the unique values from 1st list is 10 20 30 40 the unique values from 2nd list is 1 2 3 4 5
Time Complexity:O(N)
Auxiliary Space: O(N)
Method#7: Using pandas
Python3
import pandas as pd # function to get unique values def unique(list1): unique_list = pd.Series(list1).drop_duplicates().tolist() for x in unique_list: print (x) # driver code list1 = [ 10 , 20 , 10 , 30 , 40 , 40 ] print ( "the unique values from 1st list is" ) unique(list1) list2 = [ 1 , 2 , 1 , 1 , 3 , 4 , 3 , 3 , 5 ] print ( "\nthe unique values from 2nd list is" ) unique(list2) #This code is contributed by Vinay Pinjala. |
Output:
the unique values from 1st list is 10 20 30 40 the unique values from 2nd list is 1 2 3 4 5
Time Complexity:O(N)
Auxiliary Space: O(N)
Method #8: Using dict.fromkeys() –
Using the fromkeys() method of dictionary data structure we can fetch the unique elements.
Step – 1: Firstly we need to define a list which consists of duplicate elements.
Step – 2 : Then we need to use a variable in which we will store the result after using the fromkeys() method.
Step – 3 : We need to convert that result into list, as the fromkeys() method is part of the dictionary so by default it returns a dictionary with all the unique keys and None as their values.
Step – 4 : We will print the final result.
Python3
# python code to fetch # unique values from list # using dict.fromkeys() method # defining a list which consists # duplicate values list1 = [ 10 , 20 , 10 , 30 , 40 , 40 ] list2 = [ 1 , 2 , 1 , 1 , 3 , 4 , 3 , 3 , 5 ] # storing the result of the fromkeys() # operation and converting it into list unique_list_1 = list ( dict .fromkeys(list1)) unique_list_2 = list ( dict .fromkeys(list2)) # Printing the final result print (unique_list_1,unique_list_2,sep = "\n" ) |
[10, 20, 30, 40] [1, 2, 3, 4, 5]
Time Complexity – O(n)
Space Complexity – O(n)
Please Login to comment...