A dictionary is a data structure that stores data values in key-value pairs. They are also referred to as a map, a hash, or a HashMap. For example, we have data on students with their respective scores for a course and you are looking for a way to represent it. We would most likely represent it as a dictionary where the key would be the name of the student and the value would be the student's scores.
This representation gives us the leverage to make corrections to our data in case some information changes, such as changing the score of a particular student, changing a student's name, etc.
In this Answer, we're going to take a look at the ways in which we can remove a key from a dictionary in Julia.
There are different ways in which we can remove keys from a dictionary in Julia. Some of which are:
Using the delete!
method
Using the pop!
method
Let's see how to use both methods.
delete!
methodLet's see how to use the delete!
method to delete a key from a dictionary:
School = Dict("Mark" => 45, "Jane" => 70, "Peter" => 88)delete!(School,"Jane")println(School)
Line 1: Create an untyped dictionary named as School
that has three key-value pairs.
Line 2: Remove the the key Jane
from the dictionary School
.
Line 3: Print the dictionary.
Note: When we delete the key, we also delete the value.
pop!
methodHere, we are going to remove same dictionary but we are going to be removing "Mark" from the dictionary by using pop!
method.
School = Dict("Mark" => 45, "Jane" => 70, "Peter" => 88)pop!(School,"Mark")println(School)
Line 1: Create a dictionary named as School
.
Line 2: Remove the the key Mark
from the dictionary School
.
Line 3: Print the dictionary.
We have seen that the key with the value has been removed from the dictionary School
where key equals Mark
.
In this Answer, we have learned how to remove keys from a dictionary in Julia.