# How do you find the longest sublist within a nested list

Say, I have a list

stuff= {{eat, tea, now}, {how, did, you, say}, {the, cat, ate, was, can}, {win, din, sin}}


I have a nested list and I want to find the list that has the most words in it and then I want to find the length of those words. How would I do that?

I thought it was

Max[StringLength[stuff]]


and

Length[Max[StringLength[stuff]]]


but StringLength doesn't do me any good here since the length of each letter is the same.

Since you want to find the sublist with the maximum length (i.e. most number of words), you don't need to concern yourself with StringLength. A simple solution would be to use Pick to select the longest list and then use StringLength to get the lengths.

With[{L = Length /@ stuff}, StringLength@Pick[stuff, L, Max@L]]
(* {{3, 3, 3, 3, 3}} *)

• Would I also be able to use Select in this case? – thaibak527 Oct 25 '13 at 21:05
• Yes, you can use Select. Is there a reason you don't want to use Pick? – rm -rf Oct 25 '13 at 21:05
• Because I have only used Pick for a simple set of lists, and not really familiar with it. So, if you use Pick here, the program compares the lists, and "picks" the list with the highest amount of strings? – thaibak527 Oct 25 '13 at 21:10
• @thaibak527 Yes, it picks the sublists with the maximum length (because I told it to). – rm -rf Oct 25 '13 at 21:26
• Great great, thank you I will remember that for future – thaibak527 Oct 25 '13 at 22:00
stuff={{eat, tea, now}, {how, did, you, say}, {the, cat, ate, was, can}, {win, din, sin}};

Composition[StringLength, Map[SymbolName], First, MaximalBy[Length]] @ stuff


{3, 3, 3, 3, 3}

• +1, though I think using Map[SymolName] might make it more slick. – jjc385 Oct 7 '17 at 23:48
• @jjc385, great point; thank you. Updated with the version you suggested. – kglr Oct 7 '17 at 23:54