liveBook · Manning

Map

As explained in Julia as a Second Language

A dictionary is a mutable associative collection that maps keys to values, with each dictionary specifying or inferring the types of its keys and values.

A map, called a dictionary in Julia, stores values under unique keys. An array normally retrieves a value by its position; a dictionary retrieves it by a key such as a Roman numeral, a pizza name, or a product number. In Julia, the dictionary type is `Dict`. section 6.0 section 6.9

julia> roman_numerals =
          Dict('I' => 1, 'X' => 10, 'C' => 100,
                 'V' => 5, 'L' => 50, 'D' => 500,
                 'M' => 1000)
Dict{Char,Int64} with 7 entries:
 'M' => 1000
 'D' => 500
 'I' => 1
 'L' => 50
 'V' => 5
 'X' => 10
 'C' => 100

A Roman-numeral lookup table pairs character keys with integer values. Here, `=>` makes each pair, and `Dict` collects the pairs into a dictionary. section 6.2

Look up a value with square brackets: `roman_numerals['C']` returns `100`. The expression `'X' => 10` creates a pair, not an assignment; `=` assigns a value to a variable. The key and value of a pair are available through `first(pair)` and `last(pair)`. section 6.2

Choose a construction method that fits the data you already have:

  • Pass pairs directly, as in `Dict("two" => 2, "four" => 4)`, or pass an array of pairs. section 6.6.1
  • Pass two-element tuples, as in `Dict([("mexicana", 13.0)])`, or combine separate key and value collections with `Dict(zip(words, nums))`. section 6.6.1
  • Start with `Dict()` when types are unrestricted, or declare them with `Dict{String, Float64}()` before filling an empty dictionary. The displayed type names the key type first and value type second; an incompatible key produces an error. section 6.6.1

Use a dictionary when key-based lookup matters, especially as the collection grows. Searching an unsorted array of pairs takes longer as entries are added; dictionary lookup is designed not to grow with the number of entries. Small arrays can still be faster, and an unchanging sorted array offers another fast-search option. Unlike a named tuple, a dictionary can have entries added or removed. section 6.7 section 6.9

Find a product by number

`screwdict = Dict(screw.prodnum => screw for screw in screws)` creates a lookup from product number to screw. Then `screwdict[137]` retrieves the product directly. Product-number sets can also select matching products through set operations before the dictionary retrieves their records. section 13.5.5

A substitution cipher offers another use: `Dict(zip(alphabet, substitute))` maps each original character to a replacement. Consider the lookup direction before choosing this structure, though. Decryption needs the reverse mapping, so a forward-only dictionary is not always the most convenient representation for both operations. section 15.4.2

How Julia as a Second Language covers it

How deep it goes
Works through construction, types, lookup, and array trade-offs; leaves the mechanism behind fast lookup outside its scope.
Examples
Demonstrates Roman-numeral values, pizza prices, product-number searches, and alphabet substitution.
What it assumes
Builds on arrays and tuples while teaching dictionary keys, values, pairs, and construction from the start.
What you can do after
Apply it via Julia’s `Dict` to build typed key-value lookups and choose them over arrays when appropriate.
How it connects
Connects dictionaries to pairs, tuples, arrays, named tuples, sets, and substitution ciphers.

Also explained in

Compare how 42 books explain Map →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Julia as a Second Language. The section links point to the parts of the book it draws on.