VOOZH about

URL: https://www.geeksforgeeks.org/python/python-find-min-max-in-heterogeneous-list/

⇱ Python | Find Min/Max in heterogeneous list - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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.


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.


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:


Output
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.

  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:


Output
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:

  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.

Output
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:

  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.

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 input list.
Auxiliary space: O(n), where n is the length of the input list. 

Comment