All posts

Elixir Lists explained through q&a

This post works best when paired with the Elixir docs for a general overview of Lists.

What is an Elixir List?

A data type for storing a collection of values.

Are Lists in Elixir considered Linked Lists? What does that imply?

Yes. This means that when performing operations Elixir always begins with the first element in a List and ends with the last, another way of saying this is operations will run in linear time.

Is it faster to prepend or append a value to an Elixir List?


How do you add a value(s) to a List?

One way is by using the | operator to create a new List where the left hand side are the new values and the right hand side is the previous List.

=> wise_words = ["battlestar galactical"]
=> ["bears", "beets" | wise_words]
["bears", "beets", "battlestar galactical"]

How do you merge one or more Lists into a single List?

Using the ++ operator.

=> ["bears", "beets"] ++ ["battlestar galactical"]
["bears", "beets", "battlestar galactical"]
=> ["bears", "beets"] ++ ["battlestar galactical"] ++ ["boiled biscuits"]
["bears", "beets", "battlestar galactical", "boiled biscuits"]

How do you remove the first occurrence of a value from a List?

Using the -- operator. See the example below:

=> wise_words = ["bears", "beets", "beets", "battlestar galactical", "bears"]
=> wise_words -- ["beets", "bears"]
["beets", "battlestar galactical", "bears"]

How do you remove an element using its index?

Using the List modules delete_at method and providing an index as the second argument. The below removes the second element of the List:

=> wise_words = ["bears", "beets", "battlestar galactical"]
=> List.delete_at(wise_words, 1)
["bears", "battlestar galactical"]

How do you remove the last Item from a List?

The below example uses the Kernel length function to get the index of the last element and then deletes that element using the List.delete_at/2 function.

=> wise_words = ["bears", "beets", "battlestar galactical"]
=> last_element_index = length(wise_words) - 1
=> List.delete_at(wise_words, last_element_index)
["bears", "beets"]

What is the head of a List, what is the tail?

The head is the first element of a List and the tail is the rest of the Lists elements that follow.

How do you remove all elements with the same value from a List?

You can use the Enum module and the filter or reject functions. The following example removes all occurrences of “bears” and “beets” from the wise_words List.

wise_words = ["bears", "beets", "beets", "battlestar galactical", "bears"]
Enum.filter(wise_words, fn (word) -> (word !== "bears") && (word !== "beets") end)

How do you retrieve the value of the head of a List? How do you retrieve the tail?

  1. Using the Kernel modules hd and tl functions respectively:
    => hd ["bears", "beets", "battlestar galactical"]
    => tl ["bears", "beets", "battlestar galactical"]
    ["beets", "battlestar galactical"]
  2. Using pattern matching:
    => [head | tail] = ["bears", "beets", "battlestar galactical"]
    => head
    => tail
    ["beets", "battlestar galactical"]

More Elixir Decks:

  1. Atoms and Integers
  2. Tuples