Dictionary in
Julia is a collection of key-value pairs, where each value in the dictionary can be accessed with its key. These key-value pairs need not be of the same data type, which means a String typed key can hold a value of any type like Integer, String, float, etc. Keys of a dictionary can never be same, each key must be unique. This doesn't apply to the values, values can be same, as per need. Dictionaries by default are an unordered collection of data i.e. it doesn't maintain the order in which keys are inserted.
A dictionary is more like an array but in a dictionary, the indices can be of any type while in an array, the indices have to be integers only.
Each key in a dictionary maps to a value. Because the keys need to be unique, two keys can be mapped with two same values but two different values can't be mapped with a single key.
Syntax:
Dictionary_name = Dict("key1" => value1, "key2" => value2, ...)
Creating a Dictionary
A dictionary in Julia can be created with a pre-defined keyword Dict(). This keyword accepts key-value pairs as arguments and generates a dictionary by defining its data type based on the data type of the key-value pairs. One can also pre-define the data type of the dictionary if the data type of the values is known. This can be done by defining the data type within curly braces after the
'Dict
' keyword. This dictionary with pre-defined data types is known as
Typed Dictionary.
Syntax:
Dictionary_name = Dict{Key_datatype, Value_datatype}("Key1" => value1, "Key2" => value2, ...)
Example:
Python 1==
# Julia program to illustrate
# the use of Dictionary
# Creating an Empty dictionary
Dict1 = Dict()
println("Empty Dictionary = ", Dict1)
# Creating an Untyped Dictionary
Dict2 = Dict("a" => 1, "b" => 2, "c" => 3)
println("\nUntyped Dictionary = ", Dict2)
# Creating a Typed Dictionary
Dict3 = Dict{String, Integer}("a" => 10, "c" => 20)
println("\nTyped Dictionary = ", Dict3)
Output:
Accessing elements from a Dictionary
Elements from a dictionary can be accessed by using the keys of the dictionary. These keys are unique in a dictionary and hence each key holds a single value. A key-value pair can also be accessed with the use of for-loop.
Syntax:
Dictionary_name[key_name]
or
Dictionary_name[:key_name]
Example:
Python 1==
# Julia program to illustrate
# the use of Dictionary
# Creating a Dictionary with String keys
Dict1 = Dict("a" => 1, "b" => 2, "c" => "Hello")
# Accessing dictionary values using keys
println(Dict1["b"])
println(Dict1["c"])
# Creating a Dictionary with Integer keys
Dict2 = Dict(1 => 10, 2 => 20, 3 => "Geeks")
println(Dict2[1])
println(Dict2[3])
# Creating a Dictionary with Symbols
Dict3 = Dict(:a => 1, :b => "one")
println(Dict3[:b])
Output:
Use of get()
function:
Julia provides a pre-defined function to access elements of a Dictionary known as
get()
function. This function takes 3 arguments: dictionary name, key, and a default value to print if the key is not found.
Syntax:
get(Dictionary_name, Key_name, Default Value)
Example:
Python 1==
# Julia program to illustrate
# the use of Dictionary
# Creating a Dictionary with mixed-typed keys
Dict1 = Dict("a" => 1, "b" => 2, "c" => "Hello", 4 => 10)
# Accessing using get() function
# Passing '0' as default value
println(get(Dict1, "b", 0))
# Passing String as Default value
println(get(Dict1, "d", "Sorry, no such key"))
Output:
Accessing Keys and Values from a Dictionary:
Dictionaries in Julia allow accessing all the keys and all the values at once. This can be done with the use of pre-defined keywords
keys
and
values
.
Syntax:
Keys = keys(Dictionary_name)
Values = values(Dictionary_name)
Example:
Python 1==
# Julia program to illustrate
# the use of Dictionary
# Creating a Dictionary with mixed-typed keys
Dict1 = Dict("a" => 1, "b" => 2, "c" => "Hello", 4 => 10)
# Accessing all the Keys
# using 'keys' keyword
Keys = keys(Dict1)
println("Keys = ", Keys)
# Accessing all the Values
# using 'values' keyword
Values = values(Dict1)
println("Values = ", Values)
Output:
Printing Key-value pairs:
All the Key-value pairs of a dictionary can be printed at once, with the use of for-loop. This is done by iterating over each key of the dictionary and then accessing the respective value of that key.
Example 1: Using Dictionary as iterable object
Python 1==
# Julia program to illustrate
# the use of Dictionary
# Creating a Dictionary with mixed-typed keys
Dict1 = Dict("a" => 1, "b" => 2, "c" => "Hello", 4 => 10)
# Printing key-value pair using
# Dictionary as an iterable object
for i in Dict1
println(i)
end
Output:
Example 2: Accessing each key one by one
Python 1==
# Julia program to illustrate
# the use of Dictionary
# Creating a Dictionary with mixed-typed keys
Dict1 = Dict("a" => 1, "b" => 2, "c" => "Hello", 4 => 10)
# Printing key-value pair by
# accessing each key one-by-one
for i in keys(Dict1)
println(i, " => ", Dict1[i])
end
Output:
Example 3: By using (key, value) tuples
Python 1==
# Julia program to illustrate
# the use of Dictionary
# Creating a Dictionary with mixed-typed keys
Dict1 = Dict("a" => 1, "b" => 2, "c" => "Hello", 4 => 10)
# Printing key-value pair by
# using key and value tuples
for (i, j) in Dict1
println(i, " => ", j)
end
Output:
Modifying elements of a Dictionary
Modification of elements of a Dictionary includes the process of adding new keys, modifying existing key values, and deleting a key. Modification of elements doesn't include renaming keys of the dictionary, however, it can be done by deleting the existing key and adding another key with the same value.
Python 1==
# Julia program to illustrate
# the use of Dictionary
# Creating a Dictionary with mixed-typed keys
Dict1 = Dict("a" => 1, "b" => 2, "c" => "Hello", 4 => 10)
println("Initial Dictionary: \n", Dict1)
# Adding a new key
Dict1["d"] = 20
println("\nUpdated Dictionary after Adding new key: \n", Dict1)
# Updating existing key
Dict1["c"] = "Hello Geeks"
println("\nUpdated Dictionary after Updating a key: \n", Dict1)
# Deleting an existing key
Dict1 = delete !(Dict1, "d")
println("\nUpdated Dictionary after Deleting a key: \n", Dict1)
Output:
Dictionary Methods
Methods |
Description |
get() |
Used to return the value stored for the specified key, or the given default value if no mapping for the key is present. |
get!() |
Used to return the value stored for the specified key, or if no mapping for the key is present, store key => default, and return default. |
getkey() |
Used to return the key matching argument key if one exists in collection, otherwise return default. |
keytype() |
Used to return the key type of the specified array. |
merge() |
Used to construct a merged collection from the specified collections |
merge!() |
Used to update collection with pairs from the other collections. |
pairs() |
Returns an iterator over key=>value pairs for the specified collection that maps a set of keys to a set of values. |
valtype() |
Used to return the value type of the specified array. |
Similar Reads
Functions in Julia A function in Julia is an object that maps a tuple of arguments to a return value. Functions are fundamental building blocks in Julia, allowing you to encapsulate logic for reuse and abstraction. They can represent pure mathematical operations or perform actions that modify the state of other object
4 min read
Comments in Julia Comments are the statements in a code that are ignored by the compiler at the time of execution. These statements are written to beautify the code, providing an explanation for the steps that are used in the code. During coding, proper use of comments makes maintenance easier and finding bugs easily
2 min read
Julia - Currying Julia is a high-level, high-performance programming language designed for numerical and scientific computing. One of the key features of the language is currying, which is a technique for transforming a function that takes multiple arguments into a sequence of functions, each taking a single argumen
3 min read
Julia - Closures Julia is a high-performance programming language for technical computing, with syntax that is familiar to users of other technical computing environments. One of its features of Julia is the ability to use closures, which are functions that capture the values of their surrounding environment when th
4 min read
Julia - Call Java Functions Julia is a high-level, high-performance programming language for technical computing, with syntax that is similar to that of MATLAB or Python. Julia was designed to be used in a wide range of applications, including numerical computing, data science, machine learning, and parallel computing. Some of
4 min read
Julia Constructors Constructors in Julia are functions that are used to create objects. They are specifically used to create instances of objects of Composite Types. A composite type is a collection of named fields, which can also be treated as single values. Julia allows creating type objects as constructors when the
3 min read