How to get a max string length in nested lists
<p>They is a follow up question to my earlier post (re printing table from a list of lists)</p>

<p>I'm trying t get a string max value of the following nested list:</p>

<pre><code>tableData = [['apples', 'oranges', 'cherries', 'banana'],
             ['Alice', 'Bob', 'Carol', 'David'],
             ['dogs', 'cats', 'moose', 'goose']]

for i in tableData:
    print(len(max(i))) 
</code></pre>

<p>which gives me 7, 5, 5. But "cherries" is 8 </p>

<p>what a my missing here?
Thanks.</p>

<p>In string operations <code>max</code> will Returns the maximum alphabetical character from the string not in basics of length. So u have to do something like this.</p>

<pre><code>len(max(sum(tableData,[]),key=len))
</code></pre>

<p><code>sum(tableData,[])</code> will convert the <code>list of list</code> to <code>list</code> this helps to iterate through the list of list. </p>

<p>Length in each row</p>

<pre><code>In [1]: [len(max(i,key=len)) for i in tableData]
Out[1]: [8, 5, 5]
</code></pre>

<p>See the diffrence,</p>

<pre><code>In [2]: max(sum(tableData,[]))
Out[2]: 'oranges'
In [3]: max(sum(tableData,[]),key=len)
Out[3]: 'cherries'
</code></pre>

python