Elixir Keyword lists

Welcome to a tutorial on Elixir. Here you will learn about Keyword lists in Elixir. 

Data structures that can associate a certain value or multiple values to a key. Different programming languages call these features by different names such as dictionaries, hashes, associative arrays, and many more. However, In Elixir, there are two main associative data structures, keyword lists, and maps. The keyword lists will be treated in this tutorial.

For most functional programming languages, it is a common practice to use a list of 2-item tuples as the representation of an associative data structure. Although in Elixir, when we have a list of tuples and the first item of the tuple (the key) is an atom, then it is called a keyword list. check out the command below to understand. 

list = [{:a, 1}, {:b, 2}]

However, Elixir supports a special syntax for defining this kind of list. But, we can as well place the colon at the end of each atom and get rid of the tuples entirely. Check out the example below.

list_1 = [{:a, 1}, {:b, 2}]
list_2 = [a: 1, b: 2]
IO.puts(list_1 == list_2)

The output is: 

true

In the above code, they both represent a keyword list. Therefore, since keyword lists are also lists, thus, all operations used on lists can be used on them.

Now, to retrieve the value associated with an atom in the keyword list, pass the atom to [] after the name of the list, as shown below.

list = [a: 1, b: 2]
IO.puts(list[:a])

The output is:

1

 

There are three special characteristics of Keyword lists.

  • Keys must be atoms.
  • Keys are ordered, as specified by the developer.
  • Keys can be given more than once.

 

For keyword list manipulation, Elixir provides the Keyword module. 

Note that keyword lists are simply lists, and as such, they provide the same linear performance characteristics as lists. Also, the longer the list, the longer it will take to find a key, to count the number of items, etc. in respect to this, keyword lists are used in Elixir mainly as options. So, if we want to store many items or guarantee one-key associates with a maximum one-value, we can use maps instead.

 

Accessing a key

If we want to access values associated with a given key, the Keyword.get function can be used. This returns the first value associated with the given key. But, to get all the values, the Keyword.get_values function is used. Check out the example below.

kl = [a: 1, a: 2, b: 3] 
IO.puts(Keyword.get(kl, :a)) 
IO.puts(Keyword.get_values(kl)) 

The output is 

1
[1, 2]

 

Inserting a key

Now, to add a new value, we can use keywords.put_new function. But, if the key already exists, its value remains unchanged. This is shown below.

kl = [a: 1, a: 2, b: 3]
kl_new = Keyword.put_new(kl, :c, 5)
IO.puts(Keyword.get(kl_new, :c))

When the above program is run, it produces a new Keyword list with an additional key, c, and generates the following result shown below.

5

 

Deleting a key

Here, if we want to delete all entries for a key, the Keyword.delete is used. If we want to delete only the first entry of a key, then we use Keyword.delete_first function.

kl = [a: 1, a: 2, b: 3, c: 0]
kl = Keyword.delete_first(kl, :b)
kl = Keyword.delete(kl, :a)

IO.puts(Keyword.get(kl, :a))
IO.puts(Keyword.get(kl, :b))
IO.puts(Keyword.get(kl, :c))

The above code will delete the first b in the List and all the a in the list., and when the code is run, the output will be: 

0