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




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}} *)
  • $\begingroup$ Would I also be able to use Select in this case? $\endgroup$ – thaibak527 Oct 25 '13 at 21:05
  • $\begingroup$ Yes, you can use Select. Is there a reason you don't want to use Pick? $\endgroup$ – rm -rf Oct 25 '13 at 21:05
  • $\begingroup$ 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? $\endgroup$ – thaibak527 Oct 25 '13 at 21:10
  • $\begingroup$ @thaibak527 Yes, it picks the sublists with the maximum length (because I told it to). $\endgroup$ – rm -rf Oct 25 '13 at 21:26
  • $\begingroup$ Great great, thank you I will remember that for future $\endgroup$ – 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}

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.