Skip to content

Instantly share code, notes, and snippets.

@marcusschiesser
Created March 16, 2023 09:07
Show Gist options
  • Save marcusschiesser/d7ca03e6cbb14607778aa2a17b478071 to your computer and use it in GitHub Desktop.
Save marcusschiesser/d7ca03e6cbb14607778aa2a17b478071 to your computer and use it in GitHub Desktop.
Returns all items of an iterable with the same maximum or minimum value
def find_minimums(iterable, func):
"""Returns all items of the iterable with the same minimum value"""
minimum = None
minimums = []
for item in iterable:
if minimum is None or func(item) < minimum:
minimum = func(item)
minimums = [item]
elif func(item) == minimum:
minimums.append(item)
return minimums
def find_maximums(iterable, func):
"""Returns all items of the iterable with the same maximum value"""
maximum = None
maximums = []
for item in iterable:
if maximum is None or func(item) > maximum:
maximum = func(item)
maximums = [item]
elif func(item) == maximum:
maximums.append(item)
return maximums
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment