How to Choose Between Maps, Structs, and Keyword Lists

Understand how to decide between maps, structs, and keyword lists.

We'll cover the following

A dictionary is a data type that associates keys with values. In this short chapter, we’ll dive into structs, a special kind of map with a fixed structure, nested data structures, and how to alter fields in a map inside another map.

Choosing a dictionary type

First, though, let’s answer a common question: how do we choose an appropriate dictionary type for a particular need?

We ask ourselves these questions (in this order):

  1. Do we want to pattern match against the contents—for example, matching a dictionary that has a :name key somewhere in it? If so, we use a map.

  2. Do we want multiple entries with the same key? If so, we use the Keyword module.

  3. Do we need to guarantee that the elements are ordered? If so, again, we use the Keyword module.

  4. Do we have a fixed set of fields (that is, is the structure of the data always the same)? If so, we use a struct.

  5. Otherwise, if we’ve reached this point, we use a map.

Get hands-on with 1400+ tech skills courses.