1
$\begingroup$

How to select only even elements of a list, position-wise.

l = {1, 4, 5, 4, 1}
(* f[l] returns *)
(* Out: {4,4} *)
$\endgroup$
0

2 Answers 2

4
$\begingroup$
list = Array[a, 11];

If you want the even positions

list[[2 ;; ;; 2]]

(* {a[2], a[4], a[6], a[8], a[10]} *)

or

Last /@ Partition[list, 2]

(* {a[2], a[4], a[6], a[8], a[10]} *)
$\endgroup$
2
  • $\begingroup$ Good, now what about Odd members on their own? I was ONE-off. $\endgroup$
    – prog9910
    May 23, 2020 at 20:38
  • $\begingroup$ AHA, [[1 ;; ;; 2]] never mind. $\endgroup$
    – prog9910
    May 23, 2020 at 20:39
4
$\begingroup$

Part has a step specification:

l[[2;;-1;;2]]
$\endgroup$

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