Cody Bable


generate functionFri, 30 Jun 2023

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

def two_biggest_num(list_of_dictionaries): final_list = [] for item in list_of_dictionaries: final_list.append(item['num']) sorted_list = sorted(final_list, reverse=True) top_two_numbers = sorted_list[:2] return [item for item in list_of_dictionaries if item['num'] in top_two_numbers] list_of_dictionaries = [{'num':1}, {'num':2}, {'num':3}, {'num':4}, {'num':5}] two_biggest_num(list_of_dictionaries) # output [{'num': 5}, {'num': 4}]

Questions about programming?Chat with your personal AI assistant