Drop¶
Status: Stable
documented, exercised by the test suite and/or worked examples, with no known limitations recorded.
Description¶
Drop[list, n]
gives list with its first n elements dropped.
Drop[list, -n]
drops the last n elements.
Drop[list, {m, n}]
drops elements m through n.
Drop[list, {m, n, s}]
drops elements m through n in steps of s.
Drop[list, {m}]
drops the single element at position m.
Drop[list, spec1, spec2, ...]
drops elements at successive levels.
Notes
Negative indices count from the end; UpTo\[n\], All, and None are also accepted. Indices are 1-based; out-of-range requests leave the expression unevaluated.Examples (8)¶
Every input below was run against the current Mathilda build and its output recorded.
Basic examples (3)¶
In[1]:= First[<|"a" -> 10, "b" -> 20|>] In[1b]:= First[<||>, 0] Out[1b]= 0
In[2]:= Rest[<|"a" -> 10, "b" -> 20, "c" -> 30|>]
Out[2]= <|"b" -> 20, "c" -> 30|>
In[3]:= Take[<|"a" -> 1, "b" -> 2, "c" -> 3|>, 2]
Out[3]= <|"a" -> 1, "b" -> 2|>
Applications (5)¶
In[4]:= Drop[{a, b, c, d, e}, 2]
Out[4]= {c, d, e}
In[5]:= Drop[{a, b, c, d, e}, -2]
Out[5]= {a, b, c}
In[6]:= Drop[{a, b, c, d, e}, {2, 4}]
Out[6]= {a, e}
In[7]:= Drop[{a, b, c, d, e, f, g}, {2, 7, 2}]
Out[7]= {a, c, e, g}
In[8]:= Drop[{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, {2}, {2}]
Out[8]= {{1, 3}, {7, 9}}
Implementation notes¶
Algorithm. builtin_drop is the complement of Take: it removes the
elements selected by one or more sequence specifications and keeps the rest. Each
spec is resolved by get_seq_spec_indices into a concrete 1-based index list ā
supporting a plain count n (front) or -n (back), {m, n} (a contiguous
block), {m, n, s} (a strided slice), {m} (a single element), All, None,
and UpTo[k]. apply_take_drop then recurses: at each level it marks the
selected indices for removal and rebuilds the enclosing function from the
survivors, descending into successive specs for multi-dimensional drops (so
Drop[matrix, {2}, {2}] deletes a row and a column at once).
Data structures. A boolean keep mask per level plus the computed index
array; the result reuses the original head, deep-copying the surviving children.
An NDArray first argument takes the native buffer path ndstruct_drop ā a
contiguous leading-axis slice on the machine buffer, no boxing.
Complexity / limits. O(n) per level in the length of that level. Indices are
1-based, negative indices count from the end, and an out-of-range or invalid spec
returns NULL, leaving the expression unevaluated.
Attributes: NHoldRest, Protected.
References¶
See also: First, Last, Rest, Most, Take
- Source:
src/list/take_drop.c - Specification:
docs/spec/builtins/data-structures.md - Tests:
tests/test_assoc_forms.c - Tests:
tests/test_assoc_read.c - Tests:
tests/test_association.c - Tests:
tests/test_compile.c
Notes & additional examples¶
Notes¶
Drop is the complement of Take. A plain count drops from the front
(Drop[list, n]) or, with a negative count, from the back. The {m, n} form
drops a contiguous block, {m, n, s} drops a strided slice, and {m} drops a
single element. Multiple level specifications drop along successive list
dimensions, so the {2}, {2} example deletes the second row and the second
column of a matrix in one call. Indices are 1-based and negative indices count
from the end; out-of-range requests leave the expression unevaluated.