└── Find N Longest Strings in a Python List /Find N Longest Strings in a Python List: -------------------------------------------------------------------------------- 1 | # A function that finds n longest words in a list of strings 2 | def find_longest(strings, n): 3 | l = len(strings) 4 | # If n is more or equal to the list length, return the whole list. 5 | if n >= l: 6 | return strings 7 | else: 8 | sorted_strings = sorted(strings, key=len) 9 | return sorted_strings[-n:] 10 | 11 | 12 | # Example run 13 | names = ["Alice", "Bob", "Charlie", "David", "Emmanuel", "Frank", "Gabriel"] 14 | 15 | print(find_longest(names, 3)) 16 | --------------------------------------------------------------------------------