Home > Enterprise >  How to fix my code into finding the max value in each nested list of a list (Python)
How to fix my code into finding the max value in each nested list of a list (Python)

Time:10-28

I'm a beginner to python and I'm stuck on this assignment which requires me to write a function that returns a list of max integers of each nested list. I managed to write a code that could find the max value among all the nested lists (screenshot below) but couldn't figure out how to fix the code to what I want.

screenshot:

enter image description here

For reference, this is the prompt

Write a Python function larger( L ) that accepts a list which contains nested lists of integers. It should return a list of integers containing the largest of each inner (nested) list.

CodePudding user response:

This is an obvious case for a list comprehension:

def find_maxes(big_list):
    return [max(sublist) for sublist in big_list]

CodePudding user response:

Simpler function and does the job

def larger(L):
    outputList = []
    for i in L:
        outputList.append(max(i))
    return outputList
  • Related