Reference · Lists

drop

signature
drop(list | str | null, int) -> list | str

What it does

The list without its first n items, or the string without its first n characters, as a new value. Dropping more than there is gives an empty one. null counts as an empty list and gives [].

A negative n counts as 0, unlike slice, so a count worked out by subtraction stays safe.

Examples

print(drop([1, 2, 3], 2))print(drop("hello", 2))print(drop(["header", "row 1", "row 2"], 1))
output
[3]llo["row 1", "row 2"]

Edge cases

print(drop([1, 2, 3], 10), drop([1, 2], 0), drop([1, 2], -1))print(drop("añb", 2), drop("hi", 5) == "", drop("", 1) == "")print(drop(null, 2), drop([1, 2, 3], 1) == slice([1, 2, 3], 1))
output
[] [1, 2] [1, 2]b true true[] true

Errors

A first argument that is not a list, a string or null, or a count that is not an int, is a runtime error, not an error value. The count is required.

this one fails
print(drop({ a: 1 }, 1))
output
line 1: `drop` expects a list as argument 1, got map
this one fails
print(drop([1, 2]))
output
line 1: `drop` expects an int as argument 2, got nothing

Chaining

In a pipeline the list comes first and the count is the only argument: xs | drop(n). With take it pages through a list; after a sort it skips the smallest.

let page = 2let size = 3print(range(10) | drop((page - 1) * size) | take(size))print([5, 3, 9, 1] | sort | drop(1))print("id-42" | drop(3) | int)
output
[3, 4, 5][3, 5, 9]42

See also

take, slice, last, first

Thread
6Useful? Like it, or add to its thread below.

Thread 0 posts

On drop(): questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.

    Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo