Dictionaries

Dictionaries is data structure, in which each value has it§s own name. This type of structure is called a mapping.

Dictionaries are constructed to be easilly searchable based on it's key.

A dictionary is more appropriate than a list in some situations.

  • Storing file modification times, with file names as keys

  • Address book

For example list of people:

>>> names = ['Alice', 'Beth', 'Cecil', 'Dee-Dee', 'Earl']

In case that you would like to store telephone number of users using list, you would have to create second list with numbers under same index as user name:

>>> numbers = ['2341', '9102', '3158', '0142', '5551']

And lookup for user phone like this:

>>> numbers[names.index('Alice')]
'2341'

As you can see, it's not straightforward. You can use dictionay instead in such case.

phonebook = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}

Dictionaries consist of pairs of keys and their corresponding values.

You can create empty dictionary like this:

new_dictionary = {}

Keys are unique within a dictionary!

The dict() function

You can use dict function() to construct dictionary from other sequences like this:

>>> items = [('name', 'Gumby'), ('age', 42)]                
>>> d = dict(items)                
>>> d                
{'age': 42, 'name': 'Gumby'}                
>>> d['name']                
'Gumby'

You can also use keywoard argument:

>>> d = dict(name='Gumby', age=42)                
>>> d                
{'age': 42, 'name': 'Gumby'}

Basic dictionary operations

  • len(d) - returns the number of items (key-value pairs)
  • d[k] - returns the value associated with the key k
  • d[k] = v - associates the value v with the key k
  • del d[k] - deletes the item with key k
  • k in d - checks whether there is an item in d that has the key k
# A simple database

# A dictionary with person names as keys. Each person is represented as
# another dictionary with the keys 'phone' and 'addr' referring to their phone
# number and address, respectively.
people = {

    'Alice': {
        'phone': '2341',
        'addr': 'Foo drive 23'
    },

    'Beth': {
        'phone': '9102',
        'addr': 'Bar street 42'
    },

    'Cecil': {
        'phone': '3158',
        'addr': 'Baz avenue 90'
    }

}

# Descriptive labels for the phone number and address. These will be used
# when printing the output.
labels = {
    'phone': 'phone number',
    'addr': 'address'
}

name = input('Name: ')

# Are we looking for a phone number or an address?
request = input('Phone number (p) or address (a)? ')

# Use the correct key:
if request == 'p': key = 'phone'
if request == 'a': key = 'addr'

# Only try to print information if the name is a valid key in                                                
# our dictionary:
if name in people: print("{}'s {} is {}.".format(name, labels[key], people[name][key]))

String formatting with dictionaries

The dictionary may contain all kinds of information, and your format string will only pick out whatever it needs.

>>> phonebook
{'Beth': '9102', 'Alice': '2341', 'Cecil': '3258'}
>>> "Cecil's phone number is {Cecil}.".format_map(phonebook)
"Cecil's phone number is 3258."

Another example:

>>> template = '''<html>
... <head><title>{title}</title></head>
... <body>
... <h1>{title}</h1>
... <p>{text}</p>
... </body>'''
>>> data = {'title': 'My Home Page', 'text': 'Welcome to my home page!'}
>>> print(template.format_map(data))
<html>
<head><title>My Home Page</title></head>
<body>
<h1>My Home Page</h1>
<p>Welcome to my home page!</p>
</body>

Dictionary methods

clear()

The clear method removes all items from the dictionary. This is an in-place operation.

>>> d = {}
>>> d['name'] = 'Gumby'
>>> d['age'] = 42
>>> d
{'age': 42, 'name': 'Gumby'}
>>> returned_value = d.clear()
>>> d
{}
>>> print(returned_value)
None

Why don't erase dictionary simply like this?

>>> x = {}
>>> x[1] = 'one'
>>> x
{1: 'one'}
>>>
>>> x = {}
>>> x
{}

Sometimes you want also delete all referenced objects values as well:

>>> x = {}
>>> y = x
>>> x['key'] = 'value'
>>> y
{'key': 'value'}
>>> x = {}
>>> x = {}
{'key': 'value'}

vs.

>>> x = {}
>>> y = x
>>> x['key'] = 'value'
>>> y
{'key': 'value'}
>>> x.clear()
>>> y
{}

copy()

The copy method returns a new dictionary with the same key-value pairs (shallow copy).

>>> x = {'username': 'admin', 'machines': ['foo', 'bar', 'baz']}
>>> y = x.copy()
>>> y['username'] = 'mlh'
>>> y['machines'].remove('bar')
>>> y
{'username': 'mlh', 'machines': ['foo', 'baz']}
>>> x
{'username': 'admin', 'machines': ['foo', 'baz']}

deepcopy()

>>> from copy import deepcopy
>>> d = {}
>>> d['names'] = ['Alfred', 'Bertrand']
>>> c = d.copy()
>>> dc = deepcopy(d)
>>> d['names'].append('Clive')
>>> c
{'names': ['Alfred', 'Bertrand', 'Clive']}
>>> dc
{'names': ['Alfred', 'Bertrand']}

get()

Ordinarily, when you try to access an item that is not present in the dictionary, things go very wrong .

>>> d = {}
>>> print(d['name'])
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
KeyError: 'name'

Get, on the other side:

>>> print(d.get('name'))
None

items()

The items method returns all the items of the dictionary as a list of items in which each item is of the form (key, value).

>>> d = {'title': 'Python Web Site', 'url': 'http://www.python.org', 'spam': 0}
>>> d.items()
dict_items([('url', 'http://www.python.org'), ('spam', 0), ('title', 'Python Web Site')])

keys()

The keys method returns a dictionary view of the keys in the dictionary .

pop()

The pop method can be used to get the value corresponding to a given key and then to remove the key-value pair from the dictionary .

>>> d = {'x': 1, 'y': 2}
>>> d.pop('x')
1
>>> d
{'y': 2}

values()

The values method returns a dictionary view of the values in the dictionary. Unlike keys, the view returned by values may contain duplicates .

>>> d = {}
>>> d[1] = 1
>>> d[2] = 2
>>> d[3] = 3
>>> d[4] = 1
>>> d.values()
dict_values([1, 2, 3, 1])
{
  "data": {
    "sessionMaterial": {
      "id": "session-material:2019/tieto-ostrava-jaro:dictionaries:0",
      "title": "Dictionaries",
      "html": "\n          \n    \n\n    <h2>Dictionaries</h2>\n<p>Dictionaries is data structure, in which each value has it&#xA7;s own name. This type of structure is called a mapping.</p>\n<p>Dictionaries are constructed to be easilly searchable based on it&apos;s key.</p>\n<p>A dictionary is more appropriate than a list in some situations.</p>\n<ul>\n<li><p>Storing file modification times, with file names as keys</p>\n</li>\n<li><p>Address book</p>\n</li>\n</ul>\n<p>For example list of people:</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; names = [&apos;Alice&apos;, &apos;Beth&apos;, &apos;Cecil&apos;, &apos;Dee-Dee&apos;, &apos;Earl&apos;]</code></pre></div><p>In case that you would like to store telephone number of users using list, you would have to create second list with numbers under same index as user name:</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; numbers = [&apos;2341&apos;, &apos;9102&apos;, &apos;3158&apos;, &apos;0142&apos;, &apos;5551&apos;]</code></pre></div><p>And lookup for user phone like this:</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; numbers[names.index(&apos;Alice&apos;)]\n&apos;2341&apos;</code></pre></div><p>As you can see, it&apos;s not straightforward. You can use dictionay instead in such case.</p>\n<div class=\"highlight\"><pre><code>phonebook = {&apos;Alice&apos;: &apos;2341&apos;, &apos;Beth&apos;: &apos;9102&apos;, &apos;Cecil&apos;: &apos;3258&apos;}</code></pre></div><p>Dictionaries consist of pairs of keys and their corresponding values.</p>\n<p>You can create empty dictionary like this:</p>\n<div class=\"highlight\"><pre><code>new_dictionary = {}</code></pre></div><div class=\"admonition note\"><p>Keys are unique within a dictionary!</p>\n</div><h3>The dict() function</h3>\n<p>You can use dict function() to construct dictionary from other sequences like this:</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; items = [(&apos;name&apos;, &apos;Gumby&apos;), (&apos;age&apos;, 42)]                \n&gt;&gt;&gt; d = dict(items)                \n&gt;&gt;&gt; d                \n{&apos;age&apos;: 42, &apos;name&apos;: &apos;Gumby&apos;}                \n&gt;&gt;&gt; d[&apos;name&apos;]                \n&apos;Gumby&apos;</code></pre></div><p>You can also use <em>keywoard argument</em>:</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; d = dict(name=&apos;Gumby&apos;, age=42)                \n&gt;&gt;&gt; d                \n{&apos;age&apos;: 42, &apos;name&apos;: &apos;Gumby&apos;}</code></pre></div><h3>Basic dictionary operations</h3>\n<ul>\n<li><strong>len(d)</strong> - returns the number of items (key-value pairs)</li>\n<li><strong>d[k]</strong> - returns the value associated with the key k</li>\n<li><strong>d[k] = v</strong>  - associates the value v with the key k</li>\n<li><strong>del d[k]</strong> - deletes the item with key k</li>\n<li><strong>k in d</strong> - checks whether there is an item in d that has the key k</li>\n</ul>\n<div class=\"highlight\"><pre><code># A simple database\n\n# A dictionary with person names as keys. Each person is represented as\n# another dictionary with the keys &apos;phone&apos; and &apos;addr&apos; referring to their phone\n# number and address, respectively.\npeople = {\n\n    &apos;Alice&apos;: {\n        &apos;phone&apos;: &apos;2341&apos;,\n        &apos;addr&apos;: &apos;Foo drive 23&apos;\n    },\n\n    &apos;Beth&apos;: {\n        &apos;phone&apos;: &apos;9102&apos;,\n        &apos;addr&apos;: &apos;Bar street 42&apos;\n    },\n\n    &apos;Cecil&apos;: {\n        &apos;phone&apos;: &apos;3158&apos;,\n        &apos;addr&apos;: &apos;Baz avenue 90&apos;\n    }\n\n}\n\n# Descriptive labels for the phone number and address. These will be used\n# when printing the output.\nlabels = {\n    &apos;phone&apos;: &apos;phone number&apos;,\n    &apos;addr&apos;: &apos;address&apos;\n}\n\nname = input(&apos;Name: &apos;)\n\n# Are we looking for a phone number or an address?\nrequest = input(&apos;Phone number (p) or address (a)? &apos;)\n\n# Use the correct key:\nif request == &apos;p&apos;: key = &apos;phone&apos;\nif request == &apos;a&apos;: key = &apos;addr&apos;\n\n# Only try to print information if the name is a valid key in                                                \n# our dictionary:\nif name in people: print(&quot;{}&apos;s {} is {}.&quot;.format(name, labels[key], people[name][key]))</code></pre></div><h3>String formatting with dictionaries</h3>\n<p>The dictionary may contain all kinds of information, and your format string will only pick out whatever it needs.</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; phonebook\n{&apos;Beth&apos;: &apos;9102&apos;, &apos;Alice&apos;: &apos;2341&apos;, &apos;Cecil&apos;: &apos;3258&apos;}\n&gt;&gt;&gt; &quot;Cecil&apos;s phone number is {Cecil}.&quot;.format_map(phonebook)\n&quot;Cecil&apos;s phone number is 3258.&quot;</code></pre></div><p>Another example:</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; template = &apos;&apos;&apos;&lt;html&gt;\n... &lt;head&gt;&lt;title&gt;{title}&lt;/title&gt;&lt;/head&gt;\n... &lt;body&gt;\n... &lt;h1&gt;{title}&lt;/h1&gt;\n... &lt;p&gt;{text}&lt;/p&gt;\n... &lt;/body&gt;&apos;&apos;&apos;\n&gt;&gt;&gt; data = {&apos;title&apos;: &apos;My Home Page&apos;, &apos;text&apos;: &apos;Welcome to my home page!&apos;}\n&gt;&gt;&gt; print(template.format_map(data))\n&lt;html&gt;\n&lt;head&gt;&lt;title&gt;My Home Page&lt;/title&gt;&lt;/head&gt;\n&lt;body&gt;\n&lt;h1&gt;My Home Page&lt;/h1&gt;\n&lt;p&gt;Welcome to my home page!&lt;/p&gt;\n&lt;/body&gt;</code></pre></div><h3>Dictionary methods</h3>\n<p><strong>clear()</strong></p>\n<p>The clear method removes all items from the dictionary. This is an in-place operation.</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; d = {}\n&gt;&gt;&gt; d[&apos;name&apos;] = &apos;Gumby&apos;\n&gt;&gt;&gt; d[&apos;age&apos;] = 42\n&gt;&gt;&gt; d\n{&apos;age&apos;: 42, &apos;name&apos;: &apos;Gumby&apos;}\n&gt;&gt;&gt; returned_value = d.clear()\n&gt;&gt;&gt; d\n{}\n&gt;&gt;&gt; print(returned_value)\nNone</code></pre></div><p>Why don&apos;t erase dictionary simply like this?</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; x = {}\n&gt;&gt;&gt; x[1] = &apos;one&apos;\n&gt;&gt;&gt; x\n{1: &apos;one&apos;}\n&gt;&gt;&gt;\n&gt;&gt;&gt; x = {}\n&gt;&gt;&gt; x\n{}</code></pre></div><p>Sometimes you want also delete all referenced objects values as well:</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; x = {}\n&gt;&gt;&gt; y = x\n&gt;&gt;&gt; x[&apos;key&apos;] = &apos;value&apos;\n&gt;&gt;&gt; y\n{&apos;key&apos;: &apos;value&apos;}\n&gt;&gt;&gt; x = {}\n&gt;&gt;&gt; x = {}\n{&apos;key&apos;: &apos;value&apos;}</code></pre></div><p>vs.</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; x = {}\n&gt;&gt;&gt; y = x\n&gt;&gt;&gt; x[&apos;key&apos;] = &apos;value&apos;\n&gt;&gt;&gt; y\n{&apos;key&apos;: &apos;value&apos;}\n&gt;&gt;&gt; x.clear()\n&gt;&gt;&gt; y\n{}</code></pre></div><p><strong>copy()</strong></p>\n<p>The copy method returns a new dictionary with the same key-value pairs (shallow copy).</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; x = {&apos;username&apos;: &apos;admin&apos;, &apos;machines&apos;: [&apos;foo&apos;, &apos;bar&apos;, &apos;baz&apos;]}\n&gt;&gt;&gt; y = x.copy()\n&gt;&gt;&gt; y[&apos;username&apos;] = &apos;mlh&apos;\n&gt;&gt;&gt; y[&apos;machines&apos;].remove(&apos;bar&apos;)\n&gt;&gt;&gt; y\n{&apos;username&apos;: &apos;mlh&apos;, &apos;machines&apos;: [&apos;foo&apos;, &apos;baz&apos;]}\n&gt;&gt;&gt; x\n{&apos;username&apos;: &apos;admin&apos;, &apos;machines&apos;: [&apos;foo&apos;, &apos;baz&apos;]}</code></pre></div><p><strong>deepcopy()</strong></p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; from copy import deepcopy\n&gt;&gt;&gt; d = {}\n&gt;&gt;&gt; d[&apos;names&apos;] = [&apos;Alfred&apos;, &apos;Bertrand&apos;]\n&gt;&gt;&gt; c = d.copy()\n&gt;&gt;&gt; dc = deepcopy(d)\n&gt;&gt;&gt; d[&apos;names&apos;].append(&apos;Clive&apos;)\n&gt;&gt;&gt; c\n{&apos;names&apos;: [&apos;Alfred&apos;, &apos;Bertrand&apos;, &apos;Clive&apos;]}\n&gt;&gt;&gt; dc\n{&apos;names&apos;: [&apos;Alfred&apos;, &apos;Bertrand&apos;]}</code></pre></div><p><strong>get()</strong></p>\n<p>Ordinarily, when you try to access an item that is not present in the dictionary, things go very wrong .</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; d = {}\n&gt;&gt;&gt; print(d[&apos;name&apos;])\nTraceback (most recent call last):\n  File &quot;&lt;stdin&gt;&quot;, line 1, in ?\nKeyError: &apos;name&apos;</code></pre></div><p>Get, on the other side:</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; print(d.get(&apos;name&apos;))\nNone</code></pre></div><p><strong>items()</strong></p>\n<p>The items method returns all the items of the dictionary as a list of items in which each item is of the form (key, value).</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; d = {&apos;title&apos;: &apos;Python Web Site&apos;, &apos;url&apos;: &apos;http://www.python.org&apos;, &apos;spam&apos;: 0}\n&gt;&gt;&gt; d.items()\ndict_items([(&apos;url&apos;, &apos;http://www.python.org&apos;), (&apos;spam&apos;, 0), (&apos;title&apos;, &apos;Python Web Site&apos;)])</code></pre></div><p><strong>keys()</strong></p>\n<p>The keys method returns a dictionary view of the keys in the dictionary .</p>\n<p><strong>pop()</strong></p>\n<p>The pop method can be used to get the value corresponding to a given key and then to remove the key-value pair from the dictionary .</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; d = {&apos;x&apos;: 1, &apos;y&apos;: 2}\n&gt;&gt;&gt; d.pop(&apos;x&apos;)\n1\n&gt;&gt;&gt; d\n{&apos;y&apos;: 2}</code></pre></div><p><strong>values()</strong></p>\n<p>The values method returns a dictionary view of the values in the dictionary. Unlike keys, the view returned by values may contain duplicates .</p>\n<div class=\"highlight\"><pre><code>&gt;&gt;&gt; d = {}\n&gt;&gt;&gt; d[1] = 1\n&gt;&gt;&gt; d[2] = 2\n&gt;&gt;&gt; d[3] = 3\n&gt;&gt;&gt; d[4] = 1\n&gt;&gt;&gt; d.values()\ndict_values([1, 2, 3, 1])</code></pre></div>\n\n\n        "
    }
  }
}