{"route":"/en-US-v0.13.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 array, 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.13.1/reference/scripting/#fields\">field access notation</a> (<code>.key</code>) to\naccess the value. Dictionaries can be added with the <code>+</code> operator and\n<a href=\"/en-US-v0.13.1/reference/scripting/#blocks\">joined together</a>. To check whether a key is present in\nthe dictionary, use the <code>in</code> keyword.</p>\n<p>You can iterate over the pairs in a dictionary using a <a href=\"/en-US-v0.13.1/reference/scripting/#loops\">for\nloop</a>. This will iterate in the order the pairs were\ninserted / declared.</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.13.1/assets/d41c8842a0cf678555c4f98536841783.png\" alt=\"Preview\"></div></div>","constructor":{"path":[],"name":"dictionary","title":"Construct","keywords":[],"oneliner":"Converts a value into a dictionary.","element":false,"contextual":false,"deprecation":null,"details":"<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>","example":"<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.13.1/assets/bebc0d67925f97a933ee062710eb0cd0.png\" alt=\"Preview\"></div></div>","self":false,"params":[{"name":"value","details":"<p>The value that should be converted to a dictionary.</p>","example":null,"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,"deprecation":null,"details":"<p>The number of pairs in the dictionary.</p>","example":null,"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,"deprecation":null,"details":"<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>","example":null,"self":true,"params":[{"name":"key","details":"<p>The key at which to retrieve the item.</p>","example":null,"types":["str"],"strings":[],"default":null,"positional":true,"named":false,"required":true,"variadic":false,"settable":false},{"name":"default","details":"<p>A default value to return if the key is not part of the dictionary.</p>","example":null,"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. If the dictionary already","element":false,"contextual":false,"deprecation":null,"details":"<p>Inserts a new pair into the dictionary. If the dictionary already\ncontains this key, the value is updated.</p>","example":null,"self":true,"params":[{"name":"key","details":"<p>The key of the pair that should be inserted.</p>","example":null,"types":["str"],"strings":[],"default":null,"positional":true,"named":false,"required":true,"variadic":false,"settable":false},{"name":"value","details":"<p>The value of the pair that should be inserted.</p>","example":null,"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,"deprecation":null,"details":"<p>Removes a pair from the dictionary by key and return the value.</p>","example":null,"self":true,"params":[{"name":"key","details":"<p>The key of the pair to remove.</p>","example":null,"types":["str"],"strings":[],"default":null,"positional":true,"named":false,"required":true,"variadic":false,"settable":false},{"name":"default","details":"<p>A default value to return if the key does not exist.</p>","example":null,"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,"deprecation":null,"details":"<p>Returns the keys of the dictionary as an array in insertion order.</p>","example":null,"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,"deprecation":null,"details":"<p>Returns the values of the dictionary as an array in insertion order.</p>","example":null,"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. Each","element":false,"contextual":false,"deprecation":null,"details":"<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>","example":null,"self":true,"params":[],"returns":["array"],"scope":[]}]}}}