HTML Diff
0 added 0 removed
Original 2026-01-01
Modified 2026-02-21
1 <p>Like other built-in collections, Python supports it and has their syntax for describing literals. Programmers write<strong>dictionary literals</strong>in curly brackets, separate key-value pairs by commas, and separate the key from the value by colons:</p>
1 <p>Like other built-in collections, Python supports it and has their syntax for describing literals. Programmers write<strong>dictionary literals</strong>in curly brackets, separate key-value pairs by commas, and separate the key from the value by colons:</p>
2 <p>In this example, there are both string and number keys, and one of the values is a nested dictionary. And variables, of course, can act as values and keys:</p>
2 <p>In this example, there are both string and number keys, and one of the values is a nested dictionary. And variables, of course, can act as values and keys:</p>
3 <h2>Accessing items by keys</h2>
3 <h2>Accessing items by keys</h2>
4 <p>Above, we declared a dictionary called dictionary. You can request a key value from it like this:</p>
4 <p>Above, we declared a dictionary called dictionary. You can request a key value from it like this:</p>
5 <p>There is no "BANG" key in the dictionary, so we get a KeyError exception, which is the equivalent of IndexError for dictionaries. You can check the presence of a key in the dictionary using the in operator, which you already know:</p>
5 <p>There is no "BANG" key in the dictionary, so we get a KeyError exception, which is the equivalent of IndexError for dictionaries. You can check the presence of a key in the dictionary using the in operator, which you already know:</p>
6 <p>Now, if you want to get a value by a key that may not exist, you can do it like this:</p>
6 <p>Now, if you want to get a value by a key that may not exist, you can do it like this:</p>
7 <p>However, such a secure element request is needed so often that the dictionary object has a specific .get method for this:</p>
7 <p>However, such a secure element request is needed so often that the dictionary object has a specific .get method for this:</p>
8 <p>The third method call shows how we can set the default value explicitly. When we do not specify it, the method will return None if there is no value for the specified key.</p>
8 <p>The third method call shows how we can set the default value explicitly. When we do not specify it, the method will return None if there is no value for the specified key.</p>
9 <h2>Using the keys, values, and items iterators</h2>
9 <h2>Using the keys, values, and items iterators</h2>
10 <p>If we try to iterate through the dictionary, we'll get a list of keys:</p>
10 <p>If we try to iterate through the dictionary, we'll get a list of keys:</p>
11 <p>We can achieve the same result more explicitly, which you'll need to call the .keys() method for:</p>
11 <p>We can achieve the same result more explicitly, which you'll need to call the .keys() method for:</p>
12 <p>To get the values, you need to call the .values() method:</p>
12 <p>To get the values, you need to call the .values() method:</p>
13 <p>And to get both the keys and the corresponding values at the same time, you can call the .items() method:</p>
13 <p>And to get both the keys and the corresponding values at the same time, you can call the .items() method:</p>
14 <p>Remember that dictionaries are unordered. Therefore, it is not worth building the logic of the code considering the order of the keys in the dictionary.</p>
14 <p>Remember that dictionaries are unordered. Therefore, it is not worth building the logic of the code considering the order of the keys in the dictionary.</p>