Julia dictionary
last modified November 18, 2026
In this article we work with dictionaries in Julia. The examples were run with Julia 1.13.
A dictionary is a collection of key/value pairs. Dictionaries are created with
Dict type.
Dictionaries are also called maps or associative arrays.
Simple example
In the first example, we construct a simple dictionary in Julia.
d = Dict("sk" => "Slovakia", "ru" => "Russia",
"de" => "Germany", "no" => "Norway")
println(d)
println(d["sk"])
println(d["no"])
A dictionary of a few countries is created. The values are the country names and their keys are the countries' abbreviations.
d = Dict("sk" => "Slovakia", "ru" => "Russia",
"de" => "Germany", "no" => "Norway")
A dictionary is created with the Dict type. Each item has a key and
a value; these are separated with the => characters. The pairs
are separated with commas.
println(d)
We print the whole dictionary. A Dict is unordered, so the order in
which the pairs are printed is not the order in which they were written; it may
also differ between Julia versions.
println(d["sk"]) println(d["no"])
We print the names of two countries. The countries are accessed by their keys. The keys are specified inside a pair of square brackets.
$ julia main.jl
Dict("sk" => "Slovakia", "no" => "Norway", "ru" => "Russia", "de" => "Germany")
Slovakia
Norway
Looping over a dictionary
In the next example, we go over a dictionary with for loop.
d = Dict(:a => "book", :b => "storm", :c => "pencil", :d => "green")
for (k, v) in d
println("$(k): $(v)")
end
We define a dictionary of words. The keys are symbols and the values are words. We go over the dictionary with a for loop.
for (k, v) in d
println("$(k): $(v)")
end
In each cycle, the key and the value of the current pair is stored in the
k and v variables. They are then printed to the console.
As above, the order of the pairs follows the internal layout of the dictionary.
$ julia main.jl a: book b: storm d: green c: pencil
The keys and values functions
We can use the keys and values functions to get all
the keys and values of a dictionary.
d = Dict(:a => "book", :b => "storm",
:c => "pencil", :d => "green")
println(keys(d))
println(values(d))
for e in keys(d)
println(e)
end
for e in values(d)
println(e)
end
The program uses the keys and values functions.
d = Dict(:a => "book", :b => "storm",
:c => "pencil", :d => "green")
We define a dictionary of words.
println(keys(d)) println(values(d))
The keys function returns a KeySet and the
values function returns a ValueIterator. Both are
lazy views of the dictionary and print like arrays, but neither copies the data.
$ julia main.jl [:a, :b, :d, :c] ["book", "storm", "green", "pencil"] a b d c book storm green pencil
Types
The next example works with types.
d = Dict{Symbol, String}(:a => "book", :b => "storm",
:c => "pencil", :d => "green")
println(keytype(d))
println(valtype(d))
We explicitly define the dictionary key/value types and use the
keytype and valtype functions to determine the types
of keys and values at runtime.
d = Dict{Symbol, String}(:a => "book", :b => "storm",
:c => "pencil", :d => "green")
We can explicitly specify the types for the keys and values within the
{} brackets.
$ julia main.jl Symbol String
Merging dictionaries
The merge function can be used to merge dictionaries.
d1 = Dict(:a => "book", :b => "storm", :c => "pencil") d2 = Dict(:d => "water", :e => "form", :f => "lamp") d = merge(d1, d2) println(d)
In the example, we merge two dictionaries of words with merge.
The original dictionaries are left unchanged; if both dictionaries contained
the same key, the value from the second one would win.
$ julia main.jl Dict(:a => "book", :b => "storm", :f => "lamp", :d => "water", :e => "form", :c => "pencil")
The haskey function
With the haskey function, we can determine if the dictionary
contains the given key.
d = Dict(:a => "book", :b => "storm",
:c => "pencil", :d => "green")
if haskey(d, :c)
println("the dictionary contains the :c key")
else
println("the dictionary does not contain the :c key")
end
The program checks if the dictionary contains the :c key.
The function returns a Bool, which we use in an if
statement.
$ julia main.jl the dictionary contains the :c key
The in operator
We can check if a pair is in the dictionary with the in operator.
d = Dict(:a => "book", :b => "storm",
:c => "pencil", :d => "green")
p = Pair(:a, "book")
if p in d
println("the $(p) is in the dictionary")
else
println("the item is not in the dictionary")
end
The program determines whether the defined dictionary contains the
Pair(:a, "book"). Both the key and the value must match; testing
only the key is done with haskey.
$ julia main.jl the :a => "book" is in the dictionary
We can also use the ∈ and ∉ operators. (Written with
\in and \notin).
d = Dict(:a => "book", :b => "storm",
:c => "pencil", :d => "green")
p1 = Pair(:a, "book")
if p1 ∈ d
println("the $(p1) is in the dictionary")
else
println("the item is not in the dictionary")
end
p2 = Pair(:e, "war")
if p2 ∉ d
println("the $(p2) is not in the dictionary")
else
println("the item is in the dictionary")
end
The program uses the ∈ and ∉ operators. They are
aliases for in and !(p in d).
$ julia main.jl the :a => "book" is in the dictionary the :e => "war" is not in the dictionary
The get and get! functions
The get function returns the value stored for the given key, or the
given default value if no mapping for the key is present. The get!
function returns the value stored for the given key, or if no mapping for
the key is present, it stores the given default pair and returns it.
d = Dict(:a => "book", :b => "storm", :c => "pencil",
:d => "car", :e => "book")
println(get(d, :a, "not present"))
println(d)
println(get(d, :f, "not present"))
println(d)
println(get!(d, :f, "cloud"))
println(d)
In the program we use the get and get! functions.
The first two calls only read the dictionary; the last call inserts the new
pair, as the printed dictionaries show.
$ julia main.jl book Dict(:a => "book", :b => "storm", :d => "car", :e => "book", :c => "pencil") not present Dict(:a => "book", :b => "storm", :d => "car", :e => "book", :c => "pencil") cloud Dict(:a => "book", :b => "storm", :f => "cloud", :d => "car", :e => "book", :c => "pencil")
Adding elements
In the next example, we add new pairs to a dictionary.
d = Dict(:a => "book", :b => "storm", :c => "pencil", :d => "green") println(d) push!(d, :e => "cloud") d[:x] = "sea" println(d)
In the program, we add two pairs.
push!(d, :e => "cloud")
In the first case, we use the push! function.
d[:x] = "sea"
Another way is to use the assignment operator. Assigning to an existing key replaces its value instead of adding a new pair.
$ julia main.jl Dict(:a => "book", :b => "storm", :d => "green", :c => "pencil") Dict(:a => "book", :b => "storm", :d => "green", :e => "cloud", :c => "pencil", :x => "sea")
Deleting elements
We can delete elements with delete! and pop!.
d = Dict(:a => "book", :b => "storm", :c => "pencil",
:d => "car", :e => "book")
delete!(d, :a)
println(d)
r = pop!(d)
println(r)
println(d)
In the program, we remove two elements.
delete!(d, :a)
We delete the pair that has key :a.
r = pop!(d)
The pop! removes an item in the dictionary and returns it. If
collection is an ordered container, the last item is returned; for unordered
containers, an arbitrary element is returned. We can also pass the key to
pop!(d, key) to remove a specific pair.
$ julia main.jl Dict(:b => "storm", :d => "car", :e => "book", :c => "pencil") :b => "storm" Dict(:d => "car", :e => "book", :c => "pencil")
Source
Julia Collections documentation
Julia Dictionaries documentation
In this article we have worked with dictionaries in Julia. Sets, which store unique values without keys, are covered in Julia sets; the related array and tuple collections are described in Julia arrays and Julia tuples.
Author
List all Julia tutorials.