empty!(collection) -> collection

Remove all elements from a collection.


In the Julia programming language, the function empty!(collection) is used to remove all elements from a given collection in-place.

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

Here are some common examples of using empty!:

  1. Empty an array:

    julia> arr = [10, 20, 30, 40];
    julia> empty!(arr)
    0-element Array{Int64,1}:
  2. Empty a vector of strings:

    julia> words = ["apple", "banana", "orange", "grape"];
    julia> empty!(words)
    0-element Array{String,1}:
  3. Empty a dictionary:
    julia> dict = Dict("name" => "John", "age" => 25, "city" => "New York");
    julia> empty!(dict)
    Dict{String,Any} with 0 entries

Common mistake example:

julia> empty!([1, 2, 3])
ERROR: MethodError: no method matching empty!(::Array{Int64,1})

In this example, empty! is called on a regular array, which is not a mutable collection. empty! is only applicable to mutable collections like Vector, Dict, etc.

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.

*Required Field

Checking you are not a robot: