How to find all the positions of a certain element on a list ?

M.E shared this question 2 years ago
Answered

For example, I have a list with more than 30 elements; L = { 4, 4, 4, 4, 4, 4, 4, 4, 10, 10, 10, 1, 1, 1, 1, 1, 1, 1, 2, 2, 10, 10, 10, 10, 1, 1, 1, 10, 10, 10, 10, 10}

How can I make a list containing all the positions of 10 from the list L ?

Best Answer
photo

Sequence[ If[Element[L, n] ≟ 10, n, 0], n, 1, Length[L] ] \ {0}

The used sequence command is:

Sequence( <Expression>, <Variable i>, <Start Value a>, <End Value b> )

Sequence[ If[Element[L, n] ≟ 10, n, 0], n, 1, Length[L] ]

Creates a list containing the position of 10's and others as zeros. By writing \ {0} the result is the same list without zeros.

Comments (3)

photo
1

Sequence[If[Element[L, n] ≟ 10, n, 0], n, 1, Length[L]] \ {0}

photo
1

Thanks.

Could you explain the script ? - in case I also could use it in other situations..

photo
1

Sequence[ If[Element[L, n] ≟ 10, n, 0], n, 1, Length[L] ] \ {0}

The used sequence command is:

Sequence( <Expression>, <Variable i>, <Start Value a>, <End Value b> )

Sequence[ If[Element[L, n] ≟ 10, n, 0], n, 1, Length[L] ]

Creates a list containing the position of 10's and others as zeros. By writing \ {0} the result is the same list without zeros.

photo
© 2020 International GeoGebra Institute