Sometimes, while working with data, we can have a problem in which we need to compute the list which has most number of unique elements. This can have application in many domains. Lets discuss certain ways in which this task can be performed.
Method #1 : Using list comprehension + max() + set() The combination of above functionalities can be used to solve this problem. In this, we compute the maximum occurring elements using max() and reduce the logic to uniqueness using set().
Python3
# Python3 code to demonstrate # List with most unique elements # using list comprehension + max() + set() # Initializing lists test_list1 = [ 1 , 3 , 4 , 4 , 4 , 3 , 3 , 2 , 2 , 1 ] test_list2 = [ 1 , 3 , 4 , 6 , 7 ] test_list3 = [ 4 , 5 , 4 , 3 , 6 , 7 , 8 ] # printing original lists print ("The original list 1 is : " + str (test_list1)) print ("The original list 2 is : " + str (test_list2)) print ("The original list 3 is : " + str (test_list3)) # List with most unique elements # using list comprehension + max() + set() res = [ele for ele in [ set (test_list1), set (test_list2), set (test_list3)] if len (ele) = = max ([ len (sub) for sub in [ set (test_list1), set (test_list2), set (test_list3)]])][ 0 ] # printing result print (" List with Most unique values : " + str ( list (res))) |
The original list 1 is : [1, 3, 4, 4, 4, 3, 3, 2, 2, 1] The original list 2 is : [1, 3, 4, 6, 7] The original list 3 is : [4, 5, 4, 3, 6, 7, 8] List with Most unique values : [3, 4, 5, 6, 7, 8]
Time Complexity: O(n*n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n) where n is the number of elements in the list “test_list”.
Method #2 : Using max() + set() + key This task can also be performed using combination of above functions. In this, we extract the maximum frequency list using max() and key argument len.
Python3
# Python3 code to demonstrate # List with most unique elements # using key + max() + set() # Initializing lists test_list1 = [ 1 , 3 , 4 , 4 , 4 , 3 , 3 , 2 , 2 , 1 ] test_list2 = [ 1 , 3 , 4 , 6 , 7 ] test_list3 = [ 4 , 5 , 4 , 3 , 6 , 7 , 8 ] # printing original lists print ("The original list 1 is : " + str (test_list1)) print ("The original list 2 is : " + str (test_list2)) print ("The original list 3 is : " + str (test_list3)) # List with most unique elements # using key + max() + set() temp = [ set (test_list1), set (test_list2), set (test_list3)] res = max (temp, key = len ) # printing result print (" List with Most unique values : " + str ( list (res))) |
The original list 1 is : [1, 3, 4, 4, 4, 3, 3, 2, 2, 1] The original list 2 is : [1, 3, 4, 6, 7] The original list 3 is : [4, 5, 4, 3, 6, 7, 8] List with Most unique values : [3, 4, 5, 6, 7, 8]
Time Complexity: O(n*n), where n is the length of the list test_list
Auxiliary Space: O(n) additional space of size n is created where n is the number of elements in the res list
Method #3 : Using Counter() function and conditional statements
Python3
# Python3 code to demonstrate # List with most unique elements from collections import Counter # Initializing lists test_list1 = [ 1 , 3 , 4 , 4 , 4 , 3 , 3 , 2 , 2 , 1 ] test_list2 = [ 1 , 3 , 4 , 6 , 7 ] test_list3 = [ 4 , 5 , 4 , 3 , 6 , 7 , 8 ] # printing original lists print ( "The original list 1 is :" + str (test_list1)) print ( "The original list 2 is :" + str (test_list2)) print ( "The original list 3 is :" + str (test_list3)) # List with most unique elements freq1 = Counter(test_list1) freq2 = Counter(test_list2) freq3 = Counter(test_list3) res = freq1.keys() if ( len (freq2) > = len (res)): res = freq2.keys() if ( len (freq3) > = len (res)): res = freq3.keys() res = list (res) res.sort() # printing result print ( "List with Most unique values : " + str ( list (res))) |
The original list 1 is :[1, 3, 4, 4, 4, 3, 3, 2, 2, 1] The original list 2 is :[1, 3, 4, 6, 7] The original list 3 is :[4, 5, 4, 3, 6, 7, 8] List with Most unique values : [3, 4, 5, 6, 7, 8]
Time Complexity: O(N)
Auxiliary Space: O(N)
Using Counter() + max()
Python3
#Using Counter() + max() #Count frequency of each list #Get the maximum frequency among the lists using max() function #Return the list with maximum frequency from collections import Counter def most_unique_elements(lists): freq_list = [Counter(lst) for lst in lists] max_freq = max (freq_list, key = len ) return list (max_freq.keys()) #test test_lists = [[ 1 , 3 , 4 , 4 , 4 , 3 , 3 , 2 , 2 , 1 ], [ 1 , 3 , 4 , 6 , 7 ], [ 4 , 5 , 4 , 3 , 6 , 7 , 8 ]] print (most_unique_elements(test_lists)) |
[4, 5, 3, 6, 7, 8]
Time complexity: O(nk), n is the number of lists and k is the average length of the lists
Auxiliary Space: O(nk)
Explanation:
The function takes a list of lists as input, and computes the frequency of elements in each list using Counter() function.
The function returns the list with maximum frequency among all lists.
Method #5 : Using numpy:
- Initialize three lists of integers.
- Create a new list ‘lists’ that contains all three lists.
- Create a new list ‘unique_counts’ that contains the number of unique elements in each of the lists using a list comprehension.
- Get the index of the maximum value in ‘unique_counts’ using the index() method.
- Get the list at the index found in step 4 from the ‘lists’ list.
- Print the result.
Python3
import numpy as np # Initializing lists test_list1 = [ 1 , 3 , 4 , 4 , 4 , 3 , 3 , 2 , 2 , 1 ] test_list2 = [ 1 , 3 , 4 , 6 , 7 ] test_list3 = [ 4 , 5 , 4 , 3 , 6 , 7 , 8 ] # printing original lists print ( "The original list 1 is :" + str (test_list1)) print ( "The original list 2 is :" + str (test_list2)) print ( "The original list 3 is :" + str (test_list3)) lists = [test_list1, test_list2, test_list3] unique_counts = [ len (np.unique(lst)) for lst in lists] res = lists[unique_counts.index( max (unique_counts))] # printing result print ( "List with Most unique values : " + str (res)) #This code is contributed by Jyothi Pinjala. |
Output:
The original list 1 is :[1, 3, 4, 4, 4, 3, 3, 2, 2, 1]
The original list 2 is :[1, 3, 4, 6, 7]
The original list 3 is :[4, 5, 4, 3, 6, 7, 8]
List with Most unique values : [4, 5, 4, 3, 6, 7, 8]
Time complexity:
The creation of the ‘lists’ list takes O(1) time, since we are just concatenating three lists.
The creation of the ‘unique_counts’ list takes O(n) time, where n is the total number of elements in all three lists, since we are iterating through each list and calling np.unique() on each one.
The call to index() takes O(1) time.
The retrieval of the list at the index found in step 4 takes O(1) time.
Therefore, the overall time complexity is O(n).
Auxiliary Space:
The space used by the ‘lists’ list is O(n), where n is the total number of elements in all three lists.
The space used by the ‘unique_counts’ list is O(3), since it has three elements.
The space used by the ‘res’ variable is O(k), where k is the number of unique elements in the largest list.
Therefore, the overall space complexity is O(n).
Method 6 : Using the built-in function sorted() and lambda function.
Steps
Initialize the given lists.
Create a list of sets using list comprehension.
Sort the list of sets in decreasing order of their lengths.
Return the first set from the sorted list as it will have the most unique elements.
Convert the set to a list and print the result.
Python3
# Python3 code to demonstrate # List with most unique elements # using sorted() and lambda function # Initializing lists test_list1 = [ 1 , 3 , 4 , 4 , 4 , 3 , 3 , 2 , 2 , 1 ] test_list2 = [ 1 , 3 , 4 , 6 , 7 ] test_list3 = [ 4 , 5 , 4 , 3 , 6 , 7 , 8 ] # printing original lists print ( "The original list 1 is : " + str (test_list1)) print ( "The original list 2 is : " + str (test_list2)) print ( "The original list 3 is : " + str (test_list3)) # List with most unique elements # using sorted() and lambda function res = sorted ([ set (test_list1), set (test_list2), set (test_list3)], key = lambda x: len (x), reverse = True )[ 0 ] # printing result print ( "List with Most unique values : " + str ( list (res))) |
The original list 1 is : [1, 3, 4, 4, 4, 3, 3, 2, 2, 1] The original list 2 is : [1, 3, 4, 6, 7] The original list 3 is : [4, 5, 4, 3, 6, 7, 8] List with Most unique values : [3, 4, 5, 6, 7, 8]
The time complexity of this approach is O(n log n), where n is the total number of elements in all the given lists.
The auxiliary space of this approach is O(n), where n is the total number of elements in all the given lists.