Python Practice › List Programs › Program 62
Max and min of a mixed list
Write a Python program to find the maximum and minimum numbers in a list that also contains strings.
Input
["Sqa", 6, 5, 2, "Tools"]
Output
[6, 2]
Approach
- Keep only numbers, then use max and min.
Solution
data = ["Sqa", 6, 5, 2, "Tools"]nums = [x for x in data if isinstance(x, (int, float))]print([max(nums), min(nums)])Output
[6, 2]
Line by line
Line 2
Comparing a string with a number raises TypeError, so the strings are filtered out first.
Learn the concept
New to this topic? Read the lesson, then come back.