{"route":"/en-US-v0.14.1/reference/foundations/dictionary/","title":"Dictionary","description":"Documentation for the Dictionary type.","part":null,"outline":[{"id":"summary","name":"Summary","children":[]},{"id":"example","name":"Example","children":[]},{"id":"constructor","name":"Constructor","children":[{"id":"constructor-value","name":"value","children":[]}]},{"id":"definitions","name":"Definitions","children":[{"id":"definitions-len","name":"Length","children":[]},{"id":"definitions-at","name":"At","children":[{"id":"definitions-at-key","name":"key","children":[]},{"id":"definitions-at-default","name":"default","children":[]}]},{"id":"definitions-insert","name":"Insert","children":[{"id":"definitions-insert-key","name":"key","children":[]},{"id":"definitions-insert-value","name":"value","children":[]}]},{"id":"definitions-remove","name":"Remove","children":[{"id":"definitions-remove-key","name":"key","children":[]},{"id":"definitions-remove-default","name":"default","children":[]}]},{"id":"definitions-keys","name":"Keys","children":[]},{"id":"definitions-values","name":"Values","children":[]},{"id":"definitions-pairs","name":"Pairs","children":[]}]}],"body":{"kind":"type","content":{"name":"dictionary","title":"Dictionary","keywords":[],"oneliner":"A map from string keys to values.","details":"<p>A map from string keys to values.</p>\n<p>You can construct a dictionary by enclosing comma-separated <code>key: value</code>\npairs in parentheses. The values do not have to be of the same type. Since\nempty parentheses already yield an empty array, you have to use the special\n<code>(:)</code> syntax to create an empty dictionary.</p>\n<p>A dictionary is conceptually similar to an <a href=\"/en-US-v0.14.1/reference/foundations/array/\" title=\"array\">array</a>, but it is indexed by\nstrings instead of integers. You can access and create dictionary entries\nwith the <code>.at()</code> method. If you know the key statically, you can\nalternatively use <a href=\"/en-US-v0.14.1/reference/scripting/#fields\">field access notation</a> (<code>.key</code>) to\naccess the value. To check whether a key is present in the dictionary, use\nthe <code>in</code> keyword.</p>\n<p>You can iterate over the pairs in a dictionary using a <a href=\"/en-US-v0.14.1/reference/scripting/#loops\">for\nloop</a>. This will iterate in the order the pairs were\ninserted / declared initially.</p>\n<p>Dictionaries can be added with the <code>+</code> operator and <a href=\"/en-US-v0.14.1/reference/scripting/#blocks\">joined together</a>.\nThey can also be <a href=\"/en-US-v0.14.1/reference/foundations/arguments/#spreading\">spread</a> into a function call or\nanother dictionary<sup class=\"footnote-reference\"><a href=\"#1\">1</a></sup> with the <code>..spread</code> operator. In each case, if a\nkey appears multiple times, the last value will override the others.</p>\n<h2 id=\"example\">Example</h2>\n<div class=\"previewed-code\"><pre><code><span class=\"typ-key\">#</span><span class=\"typ-key\">let</span> dict <span class=\"typ-op\">=</span> <span class=\"typ-punct\">(</span>\n  name<span class=\"typ-punct\">:</span> <span class=\"typ-str\">&quot;Typst&quot;</span><span class=\"typ-punct\">,</span>\n  born<span class=\"typ-punct\">:</span> <span class=\"typ-num\">2019</span><span class=\"typ-punct\">,</span>\n<span class=\"typ-punct\">)</span>\n\n<span class=\"typ-pol\">#</span><span class=\"typ-pol\">dict</span><span class=\"typ-punct\">.</span><span class=\"typ-pol\">name</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-punct\">#</span><span class=\"typ-punct\">(</span>dict<span class=\"typ-punct\">.</span>launch <span class=\"typ-op\">=</span> <span class=\"typ-num\">20</span><span class=\"typ-punct\">)</span>\n<span class=\"typ-pol\">#</span><span class=\"typ-pol\">dict</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">len</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-pol\">#</span><span class=\"typ-pol\">dict</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">keys</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-pol\">#</span><span class=\"typ-pol\">dict</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">values</span><span class=\"typ-punct\">(</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-pol\">#</span><span class=\"typ-pol\">dict</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">at</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;born&quot;</span><span class=\"typ-punct\">)</span> <span class=\"typ-escape\">\\</span>\n<span class=\"typ-pol\">#</span><span class=\"typ-pol\">dict</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">insert</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;city&quot;</span><span class=\"typ-punct\">,</span> <span class=\"typ-str\">&quot;Berlin&quot;</span><span class=\"typ-punct\">)</span>\n<span class=\"typ-punct\">#</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;name&quot;</span> <span class=\"typ-key\">in</span> dict<span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/e9a45e88bf57200fabfebcb669137411.png\" alt=\"Preview\"></div></div>\n<div class=\"footnote-definition\" id=\"1\"><sup class=\"footnote-definition-label\">1</sup>\n<p>When spreading into a dictionary, if all items between the parentheses\nare spread, you have to use the special <code>(:..spread)</code> syntax. Otherwise, it\nwill spread into an array.</p>\n</div>","constructor":{"path":[],"name":"dictionary","title":"Construct","keywords":[],"oneliner":"Converts a value into a dictionary.","element":false,"contextual":false,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>Converts a value into a dictionary.</p>\n<p>Note that this function is only intended for conversion of a\ndictionary-like value to a dictionary, not for creation of a dictionary\nfrom individual pairs. Use the dictionary syntax <code>(key: value)</code> instead.</p>"},{"kind":"example","content":{"body":"<div class=\"previewed-code\"><pre><code><span class=\"typ-func\">#</span><span class=\"typ-func\">dictionary</span><span class=\"typ-punct\">(</span>sys<span class=\"typ-punct\">)</span><span class=\"typ-punct\">.</span><span class=\"typ-func\">at</span><span class=\"typ-punct\">(</span><span class=\"typ-str\">&quot;version&quot;</span><span class=\"typ-punct\">)</span>\n</code></pre><div class=\"preview\"><img src=\"/en-US-v0.14.1/assets/bebc0d67925f97a933ee062710eb0cd0.png\" alt=\"Preview\"></div></div>","title":null}}],"self":false,"params":[{"name":"value","details":[{"kind":"html","content":"<p>The value that should be converted to a dictionary.</p>"}],"types":["module"],"strings":[],"default":null,"positional":true,"named":false,"required":true,"variadic":false,"settable":false}],"returns":["dictionary"],"scope":[]},"scope":[{"path":["dictionary"],"name":"len","title":"Length","keywords":[],"oneliner":"The number of pairs in the dictionary.","element":false,"contextual":false,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>The number of pairs in the dictionary.</p>"}],"self":true,"params":[],"returns":["int"],"scope":[]},{"path":["dictionary"],"name":"at","title":"At","keywords":[],"oneliner":"Returns the value associated with the specified key in the dictionary.","element":false,"contextual":false,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>Returns the value associated with the specified key in the dictionary.\nMay be used on the left-hand side of an assignment if the key is already\npresent in the dictionary. Returns the default value if the key is not\npart of the dictionary or fails with an error if no default value was\nspecified.</p>"}],"self":true,"params":[{"name":"key","details":[{"kind":"html","content":"<p>The key at which to retrieve the item.</p>"}],"types":["str"],"strings":[],"default":null,"positional":true,"named":false,"required":true,"variadic":false,"settable":false},{"name":"default","details":[{"kind":"html","content":"<p>A default value to return if the key is not part of the dictionary.</p>"}],"types":["any"],"strings":[],"default":null,"positional":false,"named":true,"required":false,"variadic":false,"settable":false}],"returns":["any"],"scope":[]},{"path":["dictionary"],"name":"insert","title":"Insert","keywords":[],"oneliner":"Inserts a new pair into the dictionary.","element":false,"contextual":false,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>Inserts a new pair into the dictionary. If the dictionary already\ncontains this key, the value is updated.</p>\n<p>To insert multiple pairs at once, you can just alternatively another\ndictionary with the <code>+=</code> operator.</p>"}],"self":true,"params":[{"name":"key","details":[{"kind":"html","content":"<p>The key of the pair that should be inserted.</p>"}],"types":["str"],"strings":[],"default":null,"positional":true,"named":false,"required":true,"variadic":false,"settable":false},{"name":"value","details":[{"kind":"html","content":"<p>The value of the pair that should be inserted.</p>"}],"types":["any"],"strings":[],"default":null,"positional":true,"named":false,"required":true,"variadic":false,"settable":false}],"returns":[],"scope":[]},{"path":["dictionary"],"name":"remove","title":"Remove","keywords":[],"oneliner":"Removes a pair from the dictionary by key and return the value.","element":false,"contextual":false,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>Removes a pair from the dictionary by key and return the value.</p>"}],"self":true,"params":[{"name":"key","details":[{"kind":"html","content":"<p>The key of the pair to remove.</p>"}],"types":["str"],"strings":[],"default":null,"positional":true,"named":false,"required":true,"variadic":false,"settable":false},{"name":"default","details":[{"kind":"html","content":"<p>A default value to return if the key does not exist.</p>"}],"types":["any"],"strings":[],"default":null,"positional":false,"named":true,"required":false,"variadic":false,"settable":false}],"returns":["any"],"scope":[]},{"path":["dictionary"],"name":"keys","title":"Keys","keywords":[],"oneliner":"Returns the keys of the dictionary as an array in insertion order.","element":false,"contextual":false,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>Returns the keys of the dictionary as an array in insertion order.</p>"}],"self":true,"params":[],"returns":["array"],"scope":[]},{"path":["dictionary"],"name":"values","title":"Values","keywords":[],"oneliner":"Returns the values of the dictionary as an array in insertion order.","element":false,"contextual":false,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>Returns the values of the dictionary as an array in insertion order.</p>"}],"self":true,"params":[],"returns":["array"],"scope":[]},{"path":["dictionary"],"name":"pairs","title":"Pairs","keywords":[],"oneliner":"Returns the keys and values of the dictionary as an array of pairs.","element":false,"contextual":false,"deprecationMessage":null,"deprecationUntil":null,"details":[{"kind":"html","content":"<p>Returns the keys and values of the dictionary as an array of pairs. Each\npair is represented as an array of length two.</p>"}],"self":true,"params":[],"returns":["array"],"scope":[]}]}}}