

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- max() and min() in Python
- List Methods in Python - in, not in, len(), min(), max()
- Use of min() and max() in Python
- Min-Max Heaps
- Python – Test if elements of list are in Min/Max range from other list
- Symmetric Min-Max Heaps
- How to find Min/Max numbers in a java array?
- Program to fill Min-max game tree in Python
- Program to find common fraction between min and max using given constraint in Python
- Find max and min values in array of primitives using Java
- Sorting max to min value in MySQL
- JavaScript: How to Find Min/Max Values Without Math Functions?
- Min-Max Range Queries in Array in C++
- Find max and min values in an array of primitives using Java
- How to find the min/max element of an Array in JavaScript?
Advertisements