Friday, November 1, 2024
Google search engine
HomeLanguagesPython | Find Min/Max in heterogeneous list

Python | Find Min/Max in heterogeneous list

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, 'Lazyroar', 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, 'Lazyroar', 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, 'Lazyroar', 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, 'Lazyroar', 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, 'Lazyroar', 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


Output

The original list is : [3, 'computer', 5, 'Lazyroar', 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.

  1. Initialize a variable test_list with the input list.
  2. Print the original list using the print() function.
  3. Initialize a variable min_val to None. This will hold the minimum integer value found so far, if any.
  4. Use a for loop to iterate over each element elem in test_list.
  5. Use the isinstance() function to check if elem is an integer.
  6. 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.
  7. 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, 'Lazyroar', 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")


Output

The original list is : [3, 'computer', 5, 'Lazyroar', 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:

  1. Initialize the original list.
  2. Import the itertools module.
  3. Create a new list of integers using filterfalse() function and lambda function.
  4. Find the maximum value in the new integer list using the max() function.
  5. Print the maximum value found in the list.

Python3




import itertools
 
# Initializing list
test_list = [3, 'computer', 5, 'Lazyroar', 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


Output

The original list is : [3, 'computer', 5, 'Lazyroar', 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:

  1. Initialize the list test_list with the given elements.
  2. 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.
  3. 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. 
  4. 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, 'Lazyroar', 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")


Output

The original list is : [3, 'computer', 5, 'Lazyroar', 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. 

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments