Home > Software design >  Max values in list of lists Python 3
Max values in list of lists Python 3

Time:11-30

I'm a beginner in Python 3. I have a list of lists and I need to find the maximum value of each list, but I don't know how to do it.

l = [['6', '6', '11', '12', '10', '6', '9', '10', '6'], ['4'], ['6', '20', '10', '6', '10', '7', '8'], ['8', '4', '1', '5', '5']]

The maximum values ​​have to come back in a list, because then I'll have to sum them. Example:

max_values = [12, 4, 20, 8]

Thanks for your help

CodePudding user response:

You can use a simple list comprehension with max to get the biggest value and map to convert all elements to int:

max_values = [max(map(int, i)) for i in l]

CodePudding user response:

Try using lamda and the max built-in function in combination, or map the list and use max afterwords: https://stackoverflow.com/a/18296814/13526701 Or https://stackoverflow.com/a/70155571/13526701

  • Related