I have a list consisting of strings and integers:

{"abc", 3, 4, "def", 5, 6, 7, "xz", 8, 3}

and I would like to obtain:

{{"abc", 3, 4}, {"def", 5, 6, 7}, {"xz", 8, 3}}

The general problem is how to create sublists where the first element of each is a string, and the subsequent elements are numbers until the next element is a string, at which point the this string serves as the first element of the next sublist.

I would be grateful for any guidance.


Marked as a duplicate but let me leave this CW for clarity

list  = {"abc", 3, 4, "def", 5, 6, 7, "xz", 8, 3};

Split[list, ! StringQ[#2] &]

or (V10.2+)

SequenceCases[list, {_String, __?NumberQ}]
| improve this answer | |

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