Search⌘ K

Map Traps

Explore common traps when using Elixir maps and discover the benefits of keyword lists and MapSets for specific use cases. Understand when to use each datatype, how they differ in handling order and uniqueness, and gain practical knowledge to avoid common pitfalls in Elixir data management.

These are some of the traps we might fall into if we’re not careful. Don’t be fooled by the fact that iex sorts small maps in the console for convenience. We cannot count on this ordering! If we need to enforce order, we should use lists.

Keyword lists

Keyword lists were the maps in Elixir before we had true maps. They are lists of two tuples, each with an atom key and any type for a value. They are preferred over maps due to the following function options they provide:

  • They allow duplicates.

  • They support specific syntactic ...