Skip to content

Home

N max elements

Returns the n maximum elements from the provided list.

def max_n(lst, n = 1):
  return sorted(lst, reverse = True)[:n]

max_n([1, 2, 3]) # [3]
max_n([1, 2, 3], 2) # [3, 2]

More like this

Start typing a keyphrase to see matching snippets.