Python | Find Min/Max in heterogeneous list
Last Updated :
15 May, 2023
The lists in Python can handle different type of datatypes in it. The manipulation of such lists is complicated. Let's say we have a problem in which we need to find the min/max integer value in which the list can contain string as a data type i.e heterogeneous. Let's discuss certain ways in which this can be performed.
Method #1 : Using list comprehension + min()/max() + isinstance()
This particular problem can be solved by filtering our search of min/max using the isinstance method, we can filter out the integer value and then can use min/max function to get the required min/max value.
Python3
# Python3 code to demonstrate
# Min / Max in heterogeneous list
# using list comprehension + min()/max() + isinstance()
# initializing list
test_list = [3, 'computer', 5, 'geeks', 6, 7]
# printing original list
print ("The original list is : " + str(test_list))
# using list comprehension + min()/max() + isinstance()
# Min / Max in heterogeneous list
res = min(i for i in test_list if isinstance(i, int))
# printing result
print ("The minimum value in list is : " + str(res))
Output : The original list is : [3, 'computer', 5, 'geeks', 6, 7]
The minimum value in list is : 3
Time Complexity: O(n), where n is the length of the list test_list
Auxiliary Space: O(1) constant additional space required
Method #2: Using lambda + key + max()/min() + isinstance()
The above problem can also be solved using the lambda function as a key in the min()/max() along with the isinstance method which performs the task of checking for integer values.
Python3
# Python3 code to demonstrate
# Min / Max in heterogeneous list
# using lambda + key + max()/min() + isinstance()
# initializing list
test_list = [3, 'computer', 5, 'geeks', 6, 7]
# printing original list
print ("The original list is : " + str(test_list))
# using lambda + key + max()/min() + isinstance()
# Min / Max in heterogeneous list
res = max(test_list, key = lambda i: (isinstance(i, int), i))
# printing result
print ("The maximum value in list is : " + str(res))
Output : The original list is : [3, 'computer', 5, 'geeks', 6, 7]
The maximum value in list is : 7
Time Complexity: O(n), where n is the length of test_list
Auxiliary Space: O(1)
Method #3 : Using filter() function
Here is an alternative approach using the filter function and a list comprehension:
Python3
# Initializing list
test_list = [3, 'computer', 5, 'geeks', 6, 7]
# Printing original list
print("The original list is : " + str(test_list))
# Min / Max in heterogeneous list
# using filter and list comprehension
res = list(filter(lambda x: isinstance(x, int), test_list))
# Printing result
print("The minimum value in list is : " + str(min(res)))
print("The maximum value in list is : " + str(max(res)))
# This code is contributed by Edula Vinay Kumar Reddy
OutputThe original list is : [3, 'computer', 5, 'geeks', 6, 7]
The minimum value in list is : 3
The maximum value in list is : 7
Time complexity: O(N)
Auxiliary Space: O(N)
Method 4: Using for loop
Use a loop to iterate over the list and compare each element to the current minimum or maximum value found so far.
- Initialize a variable test_list with the input list.
- Print the original list using the print() function.
- Initialize a variable min_val to None. This will hold the minimum integer value found so far, if any.
- Use a for loop to iterate over each element elem in test_list.
- Use the isinstance() function to check if elem is an integer.
- If elem is an integer, check if min_val is None (i.e., no integer has been found yet) or if elem is less than min_val. If either condition is true, set min_val to elem.
- After the loop, check if min_val is None. If it is None, print the minimum value found using the print() function. Otherwise, print a message indicating that no integer was found in the list.
Implementation:
Python3
# Initializing list
test_list = [3, 'computer', 5, 'geeks', 6, 7]
# Printing original list
print("The original list is : " + str(test_list))
# find minimum value
min_val = None
for elem in test_list:
if isinstance(elem, int):
if min_val is None or elem < min_val:
min_val = elem
# Printing result
if min_val is not None:
print("The minimum value in list is : " + str(min_val))
else:
print("No integer found in the list")
OutputThe original list is : [3, 'computer', 5, 'geeks', 6, 7]
The minimum value in list is : 3
Time Complexity: O(n)
Auxiliary Space: O(1)
Method 5:Using itertools.filterfalse() function and max() function:
- Initialize the original list.
- Import the itertools module.
- Create a new list of integers using filterfalse() function and lambda function.
- Find the maximum value in the new integer list using the max() function.
- Print the maximum value found in the list.
Python3
import itertools
# Initializing list
test_list = [3, 'computer', 5, 'geeks', 6, 7]
# Printing original list
print("The original list is : " + str(test_list))
int_list = list(itertools.filterfalse(lambda x: not isinstance(x, int), test_list))
max_value = max(int_list)
min_value = min(int_list)
# Printing result
print(f"The maximum value in list is: {max_value}")
print(f"The maximum value in list is: {min_value}")
# This code is contributed by Jyothi Pinjala
OutputThe original list is : [3, 'computer', 5, 'geeks', 6, 7]
The maximum value in list is: 7
The maximum value in list is: 3
Time Complexity: O(n) time where n is the number of items in the original list. Therefore, the time complexity of the algorithm is O(n).
Auxiliary Space: O(N), because the algorithm creates a new list of integers using the filterfalse() function, which has a space complexity of O(n) as it could potentially store all the integers from the original list. Therefore, the space complexity of the algorithm is also O(n).
Method #4: Using the built-in function filter() and the built-in function min()
In this method, we will use the built-in function filter() to filter out all the non-integer elements in the list, and then use the built-in function min() to find the minimum value in the resulting list.
Steps:
- Initialize the list test_list with the given elements.
- Use the filter() function to filter out all the non-integer elements in the list. We will pass a lambda function as the first argument to the filter() function, which will return True if the element is an integer, and False otherwise. The filter() function returns an iterator, so we need to convert it to a list using the list() function.
- Use the min() function to find the minimum value in the resulting list. If the list is empty, the min() function will raise a ValueError exception.
- We can catch this exception and print a message saying that no integer was found in the list.
Python3
# Initializing list
test_list = [3, 'computer', 5, 'geeks', 6, 7]
# Printing original list
print("The original list is : " + str(test_list))
# Finding minimum value
try:
min_val = min(filter(lambda x: isinstance(x, int), test_list))
print("The minimum value in list is : " + str(min_val))
except ValueError:
print("No integer found in the list")
OutputThe original list is : [3, 'computer', 5, 'geeks', 6, 7]
The minimum value in list is : 3
Time complexity: O(n), where n is the length of the input list.
Auxiliary space: O(n), where n is the length of the input list.
Similar Reads
Python - Maximum element in Cropped List Sometimes, while working with Python, we can have a problem in which we need to get maximum of list. But sometimes, we need to get this for between custom indices. This can be need of any domain be it normal programming or web development. Let's discuss certain ways in which this task can be perform
4 min read
Python | Maximize alternate element List The problem of getting maximum of a list is quite generic and we might some day face the issue of getting the maximum of alternate elements and get the list of 2 elements containing maximum of alternate elements. Letâs discuss certain ways in which this can be performed. Method #1 : Using list compr
3 min read
Position of maximum and minimum element in a list - Python In Python, lists are one of the most common data structures we use to store multiple items. Sometimes we need to find the position or index of the maximum and minimum values in the list. For example, consider the list li = [3, 5, 7, 2, 8, 1].The maximum element is 8, and its index is 4.The minimum e
3 min read
Python | Positions of maximum element in list Sometimes, while working with Python lists, we can have a problem in which we intend to find the position of maximum element of list. This task is easy and discussed many times. But sometimes, we can have multiple maximum elements and hence multiple maximum positions. Let's discuss a shorthand to ac
3 min read
Python | Maximum element in tuple list Sometimes, while working with data in form of records, we can have a problem in which we need to find the maximum element of all the records received. This is a very common application that can occur in Data Science domain. Letâs discuss certain ways in which this task can be performed. Method #1: U
6 min read