Cody Bable

Generation

generate functionFri, 30 Jun 2023

return list dictionary of the two biggest numbers in a list of dictionaries

def get_top_numbers(numbers): if len(numbers) == 0: return [] biggest_num = numbers[0]['number'] second_biggest_num = numbers[0]['number'] biggest_num_dict = numbers[0] second_biggest_num_dict = numbers[0] for i in range(1, len(numbers)): if biggest_num < numbers[i]['number']: second_biggest_num = biggest_num second_biggest_num_dict = biggest_num_dict biggest_num = numbers[i]['number'] biggest_num_dict = numbers[i] elif second_biggest_num < numbers[i]['number']: second_biggest_num = numbers[i]['number'] second_biggest_num_dict = numbers[i] return [biggest_num_dict, second_biggest_num_dict]

Questions about programming?Chat with your personal AI assistant