Span

i;;j

represents a span of elements i through j.

i;;

represents a span from i to the end.

;;j

represents a span from the beginning to j.

;;

represents a span that includes all elements.

i;;j;;k

represents a span from i through j in steps of k.

i;;;;k

represents a span from i to the end in steps of k.

;;j;;k

represents a span from the beginning to j in steps of k.

;;;;k

represents a span from the beginning to the end in steps of k.

Details

  • m[[i;;j;;k]] is equivalent to Take[m,{i,j,k}].
  • m[[i;;j]]=v can be used to reset a span of elements in a list or other expression.
  • When used in Part, negative i and j count from the end.
  • In i;;j, i;;j;;k, etc., i and j can be of the form UpTo[n].
  • Any argument of Span[] can be All, in which case it is interpreted as returning the largest possible number of elements consistent with any possible value of that argument.

Examples

open allclose all

Basic Examples  (3)

Take a span of elements from a list:

Take every third element:

Assign to a span of elements:

Assign a sequence of different elements:

Scope  (7)

Negative indices count from the end:

A negative step size extracts elements in reverse order:

Take all elements from the third position to the end:

Take the first five characters:

Take every other element:

Use UpTo to restrict the right endpoint of the span to the length of the list:

Use UpTo to restrict the left endpoint of the span to the length of the list:

Properties & Relations  (4)

All in the first argument of Span is equivalent to the value 1:

All in the second argument of Span is equivalent to the value -1:

All in the third argument of Span is equivalent to the value 1 or -1:

The value is chosen to given a non-empty range:

When operating on the expression expr, UpTo[n] is equivalent to Min[n,Length[expr]]:

Verify the equivalence:

In this case, only the element b is extracted:

Wolfram Research (2007), Span, Wolfram Language function, https://reference.wolfram.com/language/ref/Span.html (updated 2017).

Text

Wolfram Research (2007), Span, Wolfram Language function, https://reference.wolfram.com/language/ref/Span.html (updated 2017).

CMS

Wolfram Language. 2007. "Span." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2017. https://reference.wolfram.com/language/ref/Span.html.

APA

Wolfram Language. (2007). Span. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/Span.html

BibTeX

@misc{reference.wolfram_2024_span, author="Wolfram Research", title="{Span}", year="2017", howpublished="\url{https://reference.wolfram.com/language/ref/Span.html}", note=[Accessed: 21-November-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_span, organization={Wolfram Research}, title={Span}, year={2017}, url={https://reference.wolfram.com/language/ref/Span.html}, note=[Accessed: 21-November-2024 ]}