# Operating on a nested list

A very short version of my list named "fire" is

fire = {{0, 0, 0, 20, 0, 0}, {0, 0, 0, 20, 0, 0}, {0, 0, 20, 20, 0, 0}}.

For the first sublist Length[TakeWhile[fire[[1]], # < 20 &]], calculates the number of preceding "0" before 20 shows up. In this case I get 3. The number of preceding 0 before 20 or the end of the sublist shows up is my ultimate goal.

Since my true list has 1000 of these sublists of six numbers I tried to map the function Length[TakeWhile[fire...]. However, MapApply or Map did not work. The problem seems to identy the subsequent elements of fire within the expression. Any help is highly appreciated.

• Length@First@SplitBy[#, 20] & /@ fire Commented Aug 19, 2022 at 21:22
• f[x_] := Length[TakeWhile[x, # < 20 &]]; f /@ fire Commented Aug 19, 2022 at 21:23
• Unclear: are 0s the only possible entries before the 20? Commented Aug 19, 2022 at 21:27
• Does the built-in work? LengthWhile[#, EqualTo[0]] & /@ fire? Assuming the only possible values are 0 or 20, at least, if the lists here are good examples. Commented Aug 19, 2022 at 21:43
• Let me give my 5 cents into this discussion: Position[fire[[1]], 20][[1, 1]] - 1 yields the expected 3. Commented Aug 20, 2022 at 6:49

list = {{0, 0, 0, 20, 0, 0}, {0, 0, 0, 20, 0, 0}, {0, 0, 20, 20, 0, 0}};


Using SequenceCount

SequenceCount[#, {0 .., Except[0]}, Overlaps -> True] & /@ list


{3, 3, 2}

Another test

SequenceCount[ {{1}, {0, 1}}, {0 .., Except[0]}, Overlaps -> True] & /@ list


{0, 1}

list = {{0, 0, 0, 20, 0, 0}, {0, 0, 0, 20, 0, 0}, {0, 0, 20, 20, 0, 0}};


Using Replace at level 1 and SequencePosition:

Replace[list, x_ :> Length@SequencePosition[x, {0 .., 20}], {1}]


{3, 3, 2}