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))
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))
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
The 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.
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")
The 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:
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
The 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:
# 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")
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 input list.
Auxiliary space: O(n), where n is the length of the input list.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4