
- Python 3 Basic Tutorial
- Python 3 - Home
- What is New in Python 3
- Python 3 - Overview
- Python 3 - Environment Setup
- Python 3 - Basic Syntax
- Python 3 - Variable Types
- Python 3 - Basic Operators
- Python 3 - Decision Making
- Python 3 - Loops
- Python 3 - Numbers
- Python 3 - Strings
- Python 3 - Lists
- Python 3 - Tuples
- Python 3 - Dictionary
- Python 3 - Date & Time
- Python 3 - Functions
- Python 3 - Modules
- Python 3 - Files I/O
- Python 3 - Exceptions
Find Min-Max in heterogeneous list in Python
A python list can contain both strings and numbers. We call it a heterogeneous list. In this article we will take such a list and find the minimum and maximum number present in the list.
Finding Minimum
In this approach we will take the isinstance function to find only the integers present in the list and then apply the min function to get the minimum value out of it.
Example
listA = [12, 'Sun',39, 5,'Wed', 'Thus'] # Given list print("The Given list : ",listA) res = min(i for i in listA if isinstance(i, int)) # Result print("The minimum value in list is : ",res)
Output
Running the above code gives us the following result −
The Given list : [12, 'Sun', 39, 5, 'Wed', 'Thus'] The minimum value in list is : 5
Finding Maximum value
We take a similar approach as above. But this time we can also use the lambda function along with the max function to get the maximum value.
Example
listA = [12, 'Sun',39, 5,'Wed', 'Thus'] # Given list print("The Given list : ",listA) # use max res = max(listA, key=lambda i: (isinstance(i, int), i)) # Result print("The maximum value in list is : ",res)
Output
Running the above code gives us the following result −
The Given list : [12, 'Sun', 39, 5, 'Wed', 'Thus'] The maximum value in list is : 39
- Related Articles
- List Methods in Python - in, not in, len(), min(), max()
- max() and min() in Python
- Python – Test if elements of list are in Min/Max range from other list
- Use of min() and max() in Python
- Program to fill Min-max game tree in Python
- Min-Max Heaps
- Program to find common fraction between min and max using given constraint in Python
- How to find Min/Max numbers in a java array?
- Symmetric Min-Max Heaps
- Explain the use of MIN() and MAX() using MySQL in Python?
- Find max and min values in array of primitives using Java
- Sorting max to min value in MySQL
- Min-Max Range Queries in Array in C++
- Python - Assign a specific value to Non Max-Min elements in Tuple
- How to find the min/max element of an Array in JavaScript?

Advertisements