How to Create Dictionary in Julia?

How to Create Dictionary in Julia?

  • Julia
  • 6 mins read

A dictionary in Julia is a collection of key-value pairs, where the keys are unique and the values can be any type of data. Dictionaries are useful for storing and organizing data that is associated with specific keys, such as the elements of a database or the properties of an object. To create a dictionary in Julia, you can use the Dict constructor. The Dict constructor takes a sequence of key => value pairs as its arguments, where the keys are the keys of the dictionary and the values are the values of the dictionary. Below are some examples:

Create Dictionary in Julia Examples

In this example, the data variable is initialized to a Dict object with three key-value pairs. The keys of the dictionary are the symbols :A, :B, and :C, and the values of the dictionary are the numbers 1, the string "a", and the floating-point number 2.0, respectively.

julia> data = Dict(:A => 1, :B => "a", :C => 2.0)
Dict{Symbol,Any} with 3 entries:
  :B => "a"
  :A => 1
  :C => 2.0

Create Dictionary using zip Function in Julia

You can also create a dictionary from an array of data using the zip function. For example:

julia> data = Dict(zip([:A, :B, :C], [1, "a", 2.0]))
Dict{Symbol,Any} with 3 entries:
  :B => "a"
  :A => 1
  :C => 2.0

In this example, the data variable is initialized to a Dict object with three key-value pairs, just like in the previous example. However, in this case, the keys and values of the dictionary are generated from the zip function, which combines the elements of the two arrays into pairs of key => value elements.

Access Dictionary in Julia

Once you have created a dictionary, you can access the values of the dictionary using the dict[key] syntax, where dict is the name of the dictionary and key is the key of the value that you want to access. For example:

julia> data = Dict(:A => 1, :B => "a", :C => 2.0)
Dict{Symbol,Any} with 3 entries:
  :B => "a"
  :A => 1
  :C => 2.0

julia> data[:A]
1

In this example, the data[:A] expression returns the value associated with the :A key in the data dictionary, which is the number 1.

You can use the getindex function to access and manipulate the values of a dictionary. The getindex function allows you to use the [] syntax to access and modify the values of a dictionary. For example:

julia> data = Dict(:A => 1, :B => "a", :C => 2.0)
Dict{Symbol,Any} with 3 entries:
  :B => "a"
  :A => 1
  :C => 2.0

julia> data[:A] = 2
2

julia> data
Dict{Symbol,Any} with 3 entries:
  :B => "a"
  :A => 2
  :C => 2.0

In this example, the data[:A] expression is used to access the value associated with the :A key in the data dictionary. This value is then assigned the value 2, which updates the value of the :A key in the dictionary. You can also use the setindex! function to modify the values of a dictionary, which allows you to specify the key and value that you want to set in the dictionary. For example:

julia> data = Dict(:A => 1, :B => "a", :C => 2.0)
Dict{Symbol,Any} with 3 entries:
  :B => "a"
  :A => 1
  :C => 2.0

julia> setindex!(data, :A, 2)
Dict{Symbol,Any} with 3 entries:
  :B => "a"
  :A => 2
  :C => 2.0

In this example, the setindex!(data, :A, 2) expression updates the value of the :A key in the data dictionary to be 2. This is equivalent to the previous example, but it uses the setindex! function to make the update.

Combine Dictionaries in Julia

You can use the merge function to combine multiple dictionaries into a single dictionary. The merge function takes a sequence of dictionaries as its arguments and returns a new dictionary that contains the key-value pairs of all of the input dictionaries. For example:

julia> data1 = Dict(:A => 1, :B => "a")
Dict{Symbol,Any} with 2 entries:
  :B => "a"
  :A => 1

julia> data2 = Dict(:C => 2.0, :D => "b")
Dict{Symbol,Any} with 2 entries:
  :D => "b"
  :C => 2.0

julia> merge(data1, data2)
Dict{Symbol,Any} with 4 entries:
  :B => "a"
  :A => 1
  :D => "b"
  :C => 2.0

In this example, the merge(data1, data2) expression combines the data1 and data2 dictionaries into a new dictionary. The resulting dictionary contains all of the key-value pairs from both input dictionaries, with any duplicate keys having their values overwritten by the values in the second dictionary.

Searching in Dictionary in Julia

You can also use the in operator to check if a key is present in a dictionary. For example:

julia> data = Dict(:A => 1, :B => "a", :C => 2.0)
Dict{Symbol,Any} with 3 entries:
  :B => "a"
  :A => 1
  :C => 2.0

julia> :A in data
true

julia> :D in data
false

In this example, the :A in data expression checks if the :A key is present in the data dictionary.

Removing key-value pairs from Dictionary in Julia

you can use the delete! function to remove a key-value pair from a dictionary. The delete! function takes a dictionary and a key as its arguments and removes the key-value pair associated with the specified key from the dictionary. For example:

julia> data = Dict(:A => 1, :B => "a", :C => 2.0)
Dict{Symbol,Any} with 3 entries:
  :B => "a"
  :A => 1
  :C => 2.0

julia> delete!(data, :A)
Dict{Symbol,Any} with 2 entries:
  :B => "a"
  :C => 2.0

In this example, the delete!(data, :A) expression removes the :A key and its associated value from the data dictionary. The resulting dictionary no longer contains the :A key, and its size is reduced by one.

Using haskey function to check if key present in Dictionary

You can also use the haskey function to check if a key is present in a dictionary. The haskey function takes a dictionary and a key as its arguments and returns a Bool value indicating whether the specified key is present in the dictionary. For example:

julia> data = Dict(:A => 1, :B => "a", :C => 2.0)
Dict{Symbol,Any} with 3 entries:
  :B => "a"
  :A => 1
  :C => 2.0

julia> haskey(data, :A)
true

julia> haskey(data, :D)
false

In this example, the haskey(data, :A) expression checks if the :A key is present in the data dictionary. Since the :A key is present in the dictionary, the expression returns true. The haskey(data, :D) expression checks for the presence of the :D key in the data dictionary, which is not present, so the expression returns false.

Related:

  1. Check if a Key is in Dictionary using Julia