5
$\begingroup$

If I have {1,2,3,4,5,6}, how do I take all the second elements into a new list? So I want a list like {2,4,6}.

$\endgroup$
4
  • 5
    $\begingroup$ list={1,2,3,4,5,6}; list[[2;; ;;2]]. Extract[list, Range[2,Length@list,2]. Partition[list, 2][[All, 2]]. Etc. $\endgroup$ Commented Oct 29, 2015 at 6:46
  • $\begingroup$ If you search the docs for "take every second element", the first hit is Take, which is the function. If you use the free-form input to "take every second element from list", you get %[[1 ;; -1 ;; 2]], which is easily adapted. $\endgroup$ Commented Oct 29, 2015 at 7:06
  • $\begingroup$ The function that march and Patrick are referring to is Span, just type ?Span into your notebook to get the information on how to use it. $\endgroup$ Commented Oct 29, 2015 at 7:35
  • 2
    $\begingroup$ Guy, we should re-open this question, and make it a canonical version. Then mark this one as a duplicate. This might not be that easy to find for a beginner actually ... $\endgroup$ Commented Oct 29, 2015 at 8:40

1 Answer 1

7
$\begingroup$

The keywords are Part ([[ ... ]]) and Span (;;).

Simply use

list[[2;; ;; 2]] 
$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.