They is a follow up question to my earlier post (re printing table from a list of lists)
I'm trying t get a string max value of the following nested list:
tableData = [['apples', 'oranges', 'cherries', 'banana'],
['Alice', 'Bob', 'Carol', 'David'],
['dogs', 'cats', 'moose', 'goose']]
for i in tableData:
print(len(max(i)))
which gives me 7, 5, 5. But "cherries" is 8
what a my missing here? Thanks.
You've done the length of the maximum word. This gives the wrong answer because words are ordered lexicographically:
>>> 'oranges' > 'cherries'
True
What you probably wanted is the maximum of the lengths of words:
max(len(word) for word in i)
Or equivalently:
len(max(i, key=len))
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