Today I Learned

hashrocket A Hashrocket project

All Values For A Key In A Keyword List

A keyword list in Elixir can contain the same key multiple times.

kwl = [a: 1, b: 2, a: 3, c: 4]
#=> [a: 1, b: 2, a: 3, c: 4]

The get/2 function will only grab the value of the first occurrence.

Keyword.get(kwl, :a)
#=> 1

You can use get_values/2 to retrieve all values associated with the key.

Keyword.get_values(kwl, :a)
#=> [1, 3]
See More #elixir TILs
Looking for help? At Hashrocket, we 💜 Elixir! From our many Elixir client projects, to sponsoring the Chicago Elixir Meetup, to the source code for this application, we are invested in this community. Contact us today to talk about your Elixir project.