Python Practice › List Programs › Program 62

Max and min of a mixed list

EasyList Programs · 62 of 100

Write a Python program to find the maximum and minimum numbers in a list that also contains strings.

Advertisement
Input
["Sqa", 6, 5, 2, "Tools"]
Output
[6, 2]

Approach

  1. Keep only numbers, then use max and min.

Solution

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

More list programs

← All 100 List Programs

Leave a Comment