Home > Back-end >  how to make a function that receives a list of lists of numbers and returns the list has the highest
how to make a function that receives a list of lists of numbers and returns the list has the highest

Time:07-20

def find_biggest(lst):
    my_list = []
    my_new_list = []

how do I go from here, what do I start with to reach the largest number in all of the lists?

for example: find_biggest([[1, 2, 3], [10, -2], [1, 1, 1, 1]]) → is the list [10, -2] because 10 is the highest number value in all lists

CodePudding user response:

you can use max function with key equal to len , to get the sublist with maximum no of elements List item

return max(lst, key = max)

CodePudding user response:

You could use something like this to iterate across all lists in the list of lists and return the list with the maximum value:

def find_biggest(list_of_lists):
    biggest_num = float('-inf')
    index_of_list_with_biggest = 0

    for i in range(len(list_of_lists)):
        current_list = list_of_lists[i]
        for item in current_list:
            if(item > biggest_num):
                biggest_num = item
                index_of_list_with_biggest = i
        
    return list_of_lists[index_of_list_with_biggest]





if __name__ == "__main__":
    print(find_biggest([[1, 2, 3], [10, -2], [1, 1, 1, 1]]))

Output:

[10, -2]
  • Related