Introduction
In this challenge, you will write a function that takes any number of iterable objects or objects with a length property and returns the longest one.
This tutorial is from open-source community. Access the source code
In this challenge, you will write a function that takes any number of iterable objects or objects with a length property and returns the longest one.
Write a function longest_item(*args)
that takes any number of iterable objects or objects with a length property and returns the longest one. The function should:
max()
with len()
as the key
to return the item with the greatest length.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'
In this challenge, you learned how to write a function that takes any number of iterable objects or objects with a length property and returns the longest one. You used 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.