drop

drop(iter, n)

An iterator that generates all but the first n elements of iter.

Examples

  1. Drop elements from an array:

    julia> arr = [1, 2, 3, 4, 5];
    julia> drop(arr, 2)
    3-element Array{Int64,1}:
    3
    4
    5

    This example drops the first 2 elements from the array arr.

  2. Create a new array by dropping elements from a range:

    julia> range = 1:10;
    julia> drop(range, 5)
    6:10

    It generates a new range starting from the 6th element of the original range.

  3. Drop elements from a string:
    julia> text = "Hello, Julia!";
    julia> drop(text, 7)
    "Julia!"

    It drops the first 7 characters from the string text.

Common mistake example:

julia> arr = [1, 2, 3, 4, 5];
julia> drop(arr, 10)
ERROR: BoundsError: attempt to access 5-element Array{Int64,1} at index [6]

In this example, the number of elements to drop is greater than the size of the array. It is important to ensure that the number of elements to drop is within the valid range of the iterator or collection to avoid such errors. Always check the size of the iterator or collection before using drop.

See Also

countfrom, cycle, done, drop, eachindex, enumerate, first, repeated, rest, start, svds, take, vecdot, vecnorm, zip,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: