Python - List with most unique elements
Last Updated :
05 May, 2023
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)))
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 : [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)))
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 : [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)))
OutputThe 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))
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)))
OutputThe 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.
Similar Reads
Python Tutorial | Learn Python Programming Language Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read
Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is
8 min read