-1
list = [['apples', 'oranges', 'cherries', 'banana'],
        ['Alice', 'Bob', 'Carol', 'David'],
         ['dogs', 'cats', 'moose', 'goose']]

print(len(max(list[0])))

Output: 7

why is the output 7 and not 8?

when i modify the print statement to:

print(len(max(list[0], key = len))) 

it works. I dont understand why. I hope someone can help me out.

thanks

bungeegum
  • 17
  • 6
  • Strings are compared lexicographically by default. – Asocia Dec 22 '20 at 17:36
  • Please repeat [on topic](https://stackoverflow.com/help/on-topic) and [ask] from the [tour]. "Teach me this basic language feature" is off-topic for Stack Overflow. You have to make an honest attempt at the solution, and then ask a _specific_ question about your implementation. Stack Overflow is not intended to replace existing tutorials and documentation. – TigerhawkT3 Dec 22 '20 at 17:41

2 Answers2

1

By default, the max function returns the string with the highest value, ordered alphabetically. Which is "oranges", whose length is 8.

When you provide the key as a second parameter, it compares strings by their length and not alphabetically.

0

Because max(list[0]) equals to 'oranges' and len('oranges') equals to 7.

FrankBlack78
  • 152
  • 11