# How do I locate non-numbers

If I have a list containing both numbers and non-numbers, such as

{1,2,3,4,5,6p6,7p7,8,9}


how do I locate the positions in the list which do not contain numbers. So for the above example, I am looking for the output {{6},{7}}. I want to use the position command, but I don't know what pattern to specify.

Thanks in advance for any help.

• Position[{1,2,3,4,5,6 p6,7 p7,8,9}, Except[_?NumberQ], {1}, Heads -> False] ? Mar 21, 2022 at 14:53
• Something like Rest@Position[l, Except[_Integer], {1}]? Mar 21, 2022 at 14:53
• Ah, yes, Heads -> False is much better than Rest. Ben's answer is much more idiomatic than mine. Mar 21, 2022 at 14:54
• another option not using Position is MapIndexed[If[Not[NumericQ[lis[[First[#2]]]]], #2, Nothing] &, lis] gives {{6}, {7}} Mar 21, 2022 at 18:39

list = {1, 2, 3, 4, 5, 6 p6, 7 p7, 8, 9};


All of the following

Position[list, _?(Not@*NumericQ), {1}, Heads -> False]

Position[list, Except[_?NumericQ], {1}, Heads -> False]

Position[Map[NumericQ, list, {1}], False]


return

{{6}, {7}}

Note also that the above work for more complicated structures like the one

list = {{1, 2, 3}, {4, 5, 6 p6}, {7 p7, 8}, 9};


with a minimal modification

Position[list, _?(Not@*NumericQ), {2}, Heads -> False]

Position[list, Except[_?NumericQ], {2}, Heads -> False]

Position[Map[NumericQ, list, {2}], False]


give back

{{2, 3}, {3, 1}}

list = {1, 2, 3, 4, 5, 6 p6, 7 p7, 8, 9};


Using SequencePosition

List@*First /@ SequencePosition[list, {Except[_?NumberQ]}]


{{6}, {7}}

list = {1, 2, 3, 4, 5, 6 p6, 7 p7, 8, 9};


Using Cases:

rule = Rule[a_, b_] /; ! NumericQ[b] :> {a};

Cases[Thread @@ {Range@Length@# -> #} &@list, rule]

(*{{6}, {7}}*)


Using MapIndexed and Pick:

lst = {1, 2, 3, 4, 5, 6 p6, 7 p7, 8, 9};

Pick[##, False] & @@ Thread[MapIndexed[{#2, NumberQ[#1]} &, lst]]

(* {{6}, {7}} *)

list = {1, 2, 3, 4, 5, 6 p6, 7 p7, 8, 9};


Using SequenceSplit (new in 11.3)

p = Alternatives @@ First @ SequenceSplit[list, {_?NumberQ}]


6 p6 | 7 p7

Position[list, p]


{{6}, {7}}