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!
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'}
# 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]))
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>
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:2018/tieto:dictionaries:0", "title": "Dictionaries", "html": "\n \n \n\n <h2>Dictionaries</h2>\n<p>Dictionaries is data structure, in which each value has it§s own name. This type of structure is called a mapping.</p>\n<p>Dictionaries are constructed to be easilly searchable based on it'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>>>> names = ['Alice', 'Beth', 'Cecil', 'Dee-Dee', 'Earl']</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>>>> numbers = ['2341', '9102', '3158', '0142', '5551']</code></pre></div><p>And lookup for user phone like this:</p>\n<div class=\"highlight\"><pre><code>>>> numbers[names.index('Alice')]\n'2341'</code></pre></div><p>As you can see, it's not straightforward. You can use dictionay instead in such case.</p>\n<div class=\"highlight\"><pre><code>phonebook = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}</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>>>> items = [('name', 'Gumby'), ('age', 42)] \n>>> d = dict(items) \n>>> d \n{'age': 42, 'name': 'Gumby'} \n>>> d['name'] \n'Gumby'</code></pre></div><p>You can also use <em>keywoard argument</em>:</p>\n<div class=\"highlight\"><pre><code>>>> d = dict(name='Gumby', age=42) \n>>> d \n{'age': 42, 'name': 'Gumby'}</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 'phone' and 'addr' referring to their phone\n# number and address, respectively.\npeople = {\n\n 'Alice': {\n 'phone': '2341',\n 'addr': 'Foo drive 23'\n },\n\n 'Beth': {\n 'phone': '9102',\n 'addr': 'Bar street 42'\n },\n\n 'Cecil': {\n 'phone': '3158',\n 'addr': 'Baz avenue 90'\n }\n\n}\n\n# Descriptive labels for the phone number and address. These will be used\n# when printing the output.\nlabels = {\n 'phone': 'phone number',\n 'addr': 'address'\n}\n\nname = input('Name: ')\n\n# Are we looking for a phone number or an address?\nrequest = input('Phone number (p) or address (a)? ')\n\n# Use the correct key:\nif request == 'p': key = 'phone'\nif request == 'a': key = 'addr'\n\n# Only try to print information if the name is a valid key in \n# our dictionary:\nif name in people: print("{}'s {} is {}.".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>>>> phonebook\n{'Beth': '9102', 'Alice': '2341', 'Cecil': '3258'}\n>>> "Cecil's phone number is {Cecil}.".format_map(phonebook)\n"Cecil's phone number is 3258."</code></pre></div><p>Another example:</p>\n<div class=\"highlight\"><pre><code>>>> template = '''<html>\n... <head><title>{title}</title></head>\n... <body>\n... <h1>{title}</h1>\n... <p>{text}</p>\n... </body>'''\n>>> data = {'title': 'My Home Page', 'text': 'Welcome to my home page!'}\n>>> print(template.format_map(data))\n<html>\n<head><title>My Home Page</title></head>\n<body>\n<h1>My Home Page</h1>\n<p>Welcome to my home page!</p>\n</body></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>>>> d = {}\n>>> d['name'] = 'Gumby'\n>>> d['age'] = 42\n>>> d\n{'age': 42, 'name': 'Gumby'}\n>>> returned_value = d.clear()\n>>> d\n{}\n>>> print(returned_value)\nNone</code></pre></div><p>Why don't erase dictionary simply like this?</p>\n<div class=\"highlight\"><pre><code>>>> x = {}\n>>> x[1] = 'one'\n>>> x\n{1: 'one'}\n>>>\n>>> x = {}\n>>> x\n{}</code></pre></div><p>Sometimes you want also delete all referenced objects values as well:</p>\n<div class=\"highlight\"><pre><code>>>> x = {}\n>>> y = x\n>>> x['key'] = 'value'\n>>> y\n{'key': 'value'}\n>>> x = {}\n>>> x = {}\n{'key': 'value'}</code></pre></div><p>vs.</p>\n<div class=\"highlight\"><pre><code>>>> x = {}\n>>> y = x\n>>> x['key'] = 'value'\n>>> y\n{'key': 'value'}\n>>> x.clear()\n>>> 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>>>> x = {'username': 'admin', 'machines': ['foo', 'bar', 'baz']}\n>>> y = x.copy()\n>>> y['username'] = 'mlh'\n>>> y['machines'].remove('bar')\n>>> y\n{'username': 'mlh', 'machines': ['foo', 'baz']}\n>>> x\n{'username': 'admin', 'machines': ['foo', 'baz']}</code></pre></div><p><strong>deepcopy()</strong></p>\n<div class=\"highlight\"><pre><code>>>> from copy import deepcopy\n>>> d = {}\n>>> d['names'] = ['Alfred', 'Bertrand']\n>>> c = d.copy()\n>>> dc = deepcopy(d)\n>>> d['names'].append('Clive')\n>>> c\n{'names': ['Alfred', 'Bertrand', 'Clive']}\n>>> dc\n{'names': ['Alfred', 'Bertrand']}</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>>>> d = {}\n>>> print(d['name'])\nTraceback (most recent call last):\n File "<stdin>", line 1, in ?\nKeyError: 'name'</code></pre></div><p>Get, on the other side:</p>\n<div class=\"highlight\"><pre><code>>>> print(d.get('name'))\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>>>> d = {'title': 'Python Web Site', 'url': 'http://www.python.org', 'spam': 0}\n>>> d.items()\ndict_items([('url', 'http://www.python.org'), ('spam', 0), ('title', 'Python Web Site')])</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>>>> d = {'x': 1, 'y': 2}\n>>> d.pop('x')\n1\n>>> d\n{'y': 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>>>> d = {}\n>>> d[1] = 1\n>>> d[2] = 2\n>>> d[3] = 3\n>>> d[4] = 1\n>>> d.values()\ndict_values([1, 2, 3, 1])</code></pre></div>\n\n\n " } } }