39
$\begingroup$

I have a list:

data = {4, 5, 7, 8, 9, 5, 3, 2, 1, 2, 13, 12};

I want to take those elements in the list which are located at equal distances, say, every third element, getting the list {7, 5, 1, 12}. Can anyone suggest a way of doing this for a list having large number of elements.

$\endgroup$
8
  • 9
    $\begingroup$ l[[n;;-1;;n]] ? $\endgroup$ Commented Oct 24, 2014 at 16:28
  • $\begingroup$ Could you please explain how does it work? $\endgroup$
    – Suro
    Commented Oct 24, 2014 at 16:47
  • 2
    $\begingroup$ I would advice for solutions based on extended syntax, which are easier to understand than runes. $\endgroup$
    – rhermans
    Commented Oct 24, 2014 at 16:49
  • $\begingroup$ @Suren Did you follow the links from my answer and read the documentation? $\endgroup$
    – Szabolcs
    Commented Oct 24, 2014 at 16:49
  • $\begingroup$ Now it becomes clear. Thank you everyone for your prompt handling. $\endgroup$
    – Suro
    Commented Oct 24, 2014 at 16:53

7 Answers 7

39
$\begingroup$

Please look up Part and Span.

You can use

data[[ ;; ;; n]]
$\endgroup$
8
  • 8
    $\begingroup$ This is one of those questions that are easily answered using the documentation and should be closed as such, but at the same time would probably be really useful for new users (if only for the clear question title). $\endgroup$
    – Yves Klett
    Commented Oct 24, 2014 at 19:10
  • 1
    $\begingroup$ @YvesKlett: Could you link to where you found this easily in the documentation? $\endgroup$
    – orome
    Commented Oct 3, 2015 at 19:48
  • $\begingroup$ @ raxacoricofallapatorius Fifth entry in reference.wolfram.com/language/ref/Span.html $\endgroup$
    – Yves Klett
    Commented Oct 4, 2015 at 8:46
  • 9
    $\begingroup$ Having looked through the documentation, I didn't find it, only after finding this question, I knew what to look for. $\endgroup$
    – gyger
    Commented Feb 6, 2016 at 10:39
  • $\begingroup$ @YvesKlett - Out of the hundreds of names for functions, how are we supposed to know that 'span' is what we're looking for? I mean, it doesn't even have a name, it's like part of the list syntax. $\endgroup$
    – Quark Soup
    Commented Jun 14, 2020 at 19:20
12
$\begingroup$

I'm surprised that this has not come up:

Last /@ Partition[data, 3]

Before Span (and version 6), I used it a lot.

$\endgroup$
10
$\begingroup$

Downsample:

Downsample[array, n ] returns a downsampled version of the array by sampling every n'th element.

Downsample[data, 3]

{4, 8, 3, 2}

Partition with offset

Flatten @ Partition[data, 1, 3]

{4, 8, 3, 2}

$\endgroup$
1
  • $\begingroup$ Introduced in version 9 and I discover it only now. Definitely the most direct implementation of the answer the OP wanted. $\endgroup$ Commented Mar 8, 2020 at 13:44
9
$\begingroup$

As well as Part an Span, you could also use Take.

data = {4, 5, 7, 8, 9, 5, 3, 2, 1, 2, 13, 12};
data[[3 ;; -1 ;; 3]]
Take[data, {3, -1, 3}]

Both give

{7, 5, 1, 12}
$\endgroup$
5
$\begingroup$

Your data

data = {4, 5, 7, 8, 9, 5, 3, 2, 1, 2, 13, 12}

Let's say you want every third starting by the second element, that means you want parts {2, 5, 8, 11}. we get those indexes using Range

Range[2, Length[data], 3]
{2, 5, 8, 11}

Now we use this indexes with Part

Part[data, Range[2, Length[data], 3]]
{5, 9, 2, 13}
$\endgroup$
5
$\begingroup$

Lots of other great answers here but this is another way

Table[data[[i]], {i, 3, Length@data, 3}]
$\endgroup$
1
$\begingroup$

You can use

data = {4, 5, 7, 8, 9, 5, 3, 2, 1, 2, 13, 12};
sel = Partition[Range[3, Length@data, 3], 1]
Extract[data, sel]

But I'm sure there are shorter ways.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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