deleteat!(collection, index::Integer)
deleteat!(collection, index)
Remove the item at the given index and return the modified collection.
Subsequent items are shifted to fill the resulting gap.
julia> deleteat!([6, 5, 4, 3, 2, 1], 2)
5-element Array{Int64,1}:
6
4
3
2
1Examples
julia> foo = [6, 5, 4, 3, 2, 1];
julia> deleteat!(foo, 5);
julia> foo
5-element Array{Int64,1}:
6
5
4
3
1
julia> deleteat!(foo, 2:2:5); # sorted and unique itr
julia> foo
3-element Array{Int64,1}:
6
4
1
julia> deleteat!([6, 5, 4, 3, 2, 1], 2)
5-element Array{Int64,1}:
6
4
3
2
1
-
Delete multiple elements using an index range:
julia> arr = [10, 20, 30, 40, 50, 60]; julia> deleteat!(arr, 1:2:5) 3-element Array{Int64,1}: 50 40 60This example removes the elements at indices 1, 3, and 5 from the array
arr. - Delete elements using a tuple of indices:
julia> arr = [1, 2, 3, 4, 5, 6]; julia> deleteat!(arr, (2, 2)) ERROR: ArgumentError: indices must be unique and sortedIn this case, an error is thrown because the indices provided are not sorted and unique. Ensure that the
itrargument contains sorted and unique indices.
Common mistake example:
julia> arr = [1, 2, 3, 4, 5];
julia> deleteat!(arr, (1, 3, 3))
ERROR: ArgumentError: indices must be unique and sorted
In this example, the indices (1, 3, 3) are not unique, causing an error. The itr argument should contain unique indices in sorted order.
See Also
append!, delete!, deleteat!, empty!, endof, filter, filter!, gc, get!, getkey, haskey, insert!, isempty, keys, map, map!, merge, merge!, pop!, prepend!, push!, reduce, resize!, shift!, splice!, unshift!, values,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.