Want to learn?

Keywords lists

Representation

Documentation

You are already familiar with both lists and tuples. The keyword list is a list of two-element tuples where the first element is an atom and the second element can be any value:

[{:exit_on_close, true}, {:active, :once}, {:packet_size, 1024}]

Since it's pretty common to use keyword lists when passing options to a function, there is an alternative syntax:

[exit_on_close: true, active: :once, packet_size: 1024]

This syntax is entirely equivalent to the previous one. You can use both of these. It's also worth to note that keys (atoms) don't have to be unique:

iex> [color: "red", color: "blue"]
[color: "red", color: "blue"]

Accessing keyword list elements

You can use [] to access first occurrence of element with given atom key:

iex> list = [a: 1, b: 2, a: 3]
[a: 1, b: 2, a: 3]
iex> list[:a]
1

Adding new elements with pipe operator

You can add new element to keyword list with pipe operator:

iex> list = [a: 1, b: 2, c: 3]
[a: 1, b: 2, c: 3]
iex> list = [{:d, 4} | list]
[d: 4, a: 1, b: 2, c: 3]

Modifying keyword list elements

Just like with regular lists, you can add a new element to keyword list with ++ operator and delete with -- operator:

iex> list = [a: 1, b: 2]
[a: 1, b: 2]
iex> list = list ++ [c: 3]
[a: 1, b: 2, c: 3]
iex> list = list -- [c: 3]
[a: 1, b: 2]
Check our latest product - built from our experience helping growing businesses navigate complex compliance requirements without enterprise budgets.

humadroid.io is an affordable, all-in-one GRC platform designed for small and medium-sized businesses pursuing SOC 2 or ISO 27001 compliance. Our AI-powered compliance assistant understands your business context and transforms complex compliance work into actionable steps - generating tailored policy documentation in minutes instead of weeks, and helping draft your SOC 2 System Description in a fraction of the usual time. At just $250/month with no hidden fees or user limits, customers save 10-15 hours per week on compliance work.

Explore humadroid.io
Top

Contact us

* Required fields

The controller of your personal data provided via this contact form is Prograils sp. z o.o., with a registered seat at Sczanieckiej 9A/10, 60-215 Poznań. Your personal data will be processed in order to respond to your inquiries and for our marketing purposes (e.g. when you ask us for our post-development, maintenance or ad hoc engagements for your app). You have the rights to: access your personal data, rectify or erase your personal data, restrict the processing of your personal data, data portability and to object to the processing of your personal data. Learn more.

Notice

We do not track you online. We use only session cookies and anonymous identifiers for the purposes specified in the cookie policy. No third-party trackers.

I understand
Elo Mordo!Elo Mordo!