Files
30-seconds-of-code/snippets/longest-item.md
Angelos Chalaris f6a215e9e3 Kebab file names
2023-04-27 22:00:06 +03:00

649 B

title, tags, cover, firstSeen, lastUpdated
title tags cover firstSeen lastUpdated
Longest item list,string keyboard-tea 2019-08-20T15:27:49+03:00 2021-10-17T18:24:43+02:00

Takes any number of iterable objects or objects with a length property and returns the longest one.

  • Use max() with len() as the key to return the item with the greatest length.
  • If multiple items have the same length, the first one will be returned.
def longest_item(*args):
  return max(args, key = len)
longest_item('this', 'is', 'a', 'testcase') # 'testcase'
longest_item([1, 2, 3], [1, 2], [1, 2, 3, 4, 5]) # [1, 2, 3, 4, 5]
longest_item([1, 2, 3], 'foobar') # 'foobar'