Given a list of lists, the length of the longest list can be found with the following code.
values = [['a','a'], ['a','b','b'], ['a','b','b','a'], ['a','b','c','a']]
longest = 0
for value in values:
longest = max(longest, len(value))
print(longest)
[out]: 4
How can the length of the longest list, or the longest list be found, without a loop.
This will return the longest list in the list values
:
max(values, key=len)
This will return the length of the longest list:
max(map(len, values))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With