Professional Documents
Culture Documents
Python Dictionaries
Everything I Know
About Python...
The personal blog of author, speaker, tutor, and professional
software engineer Jeff Knupp
Python Dictionaries
Aside: one thing I dislike about the ofcial Python documentation is
that only a small percentage of entries have example code. We should
change that...)
One of the keys to becoming a better Python programmer is to have a solid
grasp of Python's built-in data structures. Using the structured format below,
today you'll learn what a dict is, when to use it, and see example code of
all of its member functions. I have some other data structures in the works,
so this may turn into a little series.
Dictionary
AKA
"Associate Array", "Map", "Hash Map", "Unordered Map"
Library
built-in
Description
Contains a series of key -> value mappings where the "key" is of any type
that is hashable (meaning it has both a __eq__() and a __hash__()
method). The "value" may be of any type and value types need not be
homogeneous.
That means, for example, we can have a dictionary where some keys map to
strings and others to ints. Probably not a great idea in practice, but there's
nothing stopping you from doing it.
https://jeffknupp.com/blog/2015/08/30/python-dictionaries/
1/13
6/26/2016
Python Dictionaries
Construction
Literal
{}: pair of braces for empty dictionary
{1:2, 3:4}: comma-separated list of the form key: value
enclosed by braces
Constructor
dict(one=2, three=4): using dict() with keyword arguments
mapping keys to values (where one and two are valid identiers)
dict([(1, 2), (3, 4)]): using dict() with an iterable
containing iterables with exactly two objects, the key and value
dict(zip([1, 3], [2, 4])): using dict() with two iterables of
equal length; the rst contains a list of keys and the second contains
their associated values.
dict({1:2, 3:4}): using dict() with the literal form as an
argument. This is silly. Why would you want this?
Mutability
mutable
Ordering
undefined
When to Use It
When describing what you want to do, if you use the word "map" (or
"match"), chances are good you need a dictionary. Use whenever a mapping
from a key to a value is required.
Example Usage
https://jeffknupp.com/blog/2015/08/30/python-dictionaries/
2/13
6/26/2016
Example Usage
Python Dictionaries
state_capitals={
'New York': 'Albany',
'New Jersey': 'Trenton',
}
Or like this:
state_im_looking_for = 'New Jersey'
my_capital = ''
for state, capital in state_capitals.items():
if state == state_im_looking_for:
my_capital = capital
Returns
https://jeffknupp.com/blog/2015/08/30/python-dictionaries/
3/13
6/26/2016
Python Dictionaries
N/A
Raises
N/A
Examples
Delete all items in a dictionary
d.clear()
d.copy()
Make a shallow copy of d. The dictionary returned by d.copy() will have
the same references as d, not copies of the items.
Returns
A new dict, representing a shallow copy of d
Raises
N/A
Examples
Create copy of a dictionary
d = {1: 'a', 2: 'b', 3: 'c'}
copied_dict = d.copy()
copied_dict # {1: 'a', 2: 'b', 3: 'c'}
d[1] = 'z'
copied_dict # {1: 'a', 2: 'b', 3: 'c'}
del k[d]
Used to remove a value from a dictionary
Returns
N/A
Raises
KeyError if key is not in dictionary
Examples
Delete entry with key 'hello'
https://jeffknupp.com/blog/2015/08/30/python-dictionaries/
4/13
6/26/2016
Python Dictionaries
dict.fromkeys(seq[, value])
Create a new dictionary with the same keys as seq. If value is provided,
each item's value is set to value. If value is not set, all item values are
set to None
Returns
N/A
Raises
N/A
Examples
Create a dictionary from a list with all values initialized to 0
my_list = [1, 2, 3]
my_dictionary = dict.fromkeys(my_list, 0)
my_dictionary # {1: 0, 2: 0, 3: 0}
d.get(key[, default)
Used to retrieve the value associated with key key. The value of default
is returned if key is not in d (rather than raising a KeyError). The
default value of default is None.
Returns
Roughly equivalent to:
https://jeffknupp.com/blog/2015/08/30/python-dictionaries/
5/13
6/26/2016
Python Dictionaries
Raises
N/A
Examples
Get a key's value or None if the key isn't present
{1: 'a', 2: 'b'}.get(3)
k in d
Used to iterate over the keys, values, or both of the dictionary.
Returns
N/A
Raises
N/A
Examples
Iterate over keys
for key in my_dictionary:
iter(d)
https://jeffknupp.com/blog/2015/08/30/python-dictionaries/
6/13
6/26/2016
Python Dictionaries
Returns
An iterator which iterates over the keys of d
Raises
StopIteration when d has no more keys
Examples
Iterate over keys
for key in my_dictionary:
d[key]
Used to access the value corresponding to the key key in d.
Returns
Value associated with the key (heterogeneous)
Raises
KeyError when key is not a member of d.
Examples
capitals = {'New York': 'Albany'}`
capital_of_ny = capitals['New York']`
print capital_of_ny`
'Albany'
len(d)
Used to determine the number of entries in a dictionary
Returns
Length of dictionary d
Raises
N/A
Examples
print 'dictionary has {} entries'.format(len(d))
k not in d
https://jeffknupp.com/blog/2015/08/30/python-dictionaries/
7/13
6/26/2016
Python Dictionaries
k not in d
Used for negative existence check. Equivalent to not key in value
Returns
True if key is not in value, False otherwise
Raises
N/A
Examples
Check for negative existence
haystack = {}
# ...
if 'needle' not in haystack:
d.keys()
Iterate over the keys in a dictionary
Returns
An iterable over all of the keys in d (in an unspecied order)
Raises
StopIteration when d has no more keys
Examples
Iterate over keys:
for key in d.keys():
d.values()
Iterate over the values in a dictionary
Returns
An iterable over all of the values in d (in an unspecied order)
Raises
StopIteration when d has no more values
Examples
https://jeffknupp.com/blog/2015/08/30/python-dictionaries/
8/13
6/26/2016
Python Dictionaries
d.items()
Iterate over the elements ((key, value) pairs) in a dictionary
Returns
An iterable over all of the (key, value) pairs in d (in an unspecied order).
Each (key, value) pairs is represented as a tuple.
Raises
StopIteration when d has no more elements
Examples
Iterate over items:
for key, value in d.items():
Note that, in the example, we can use multiple assignment to assign key to
the key and value to the value of each item directly in the for loop.
d.pop(key[, default])
Used to remove an item from a dictionary and return its associated value
Returns
d[key] if key is in d. If key is not in d but default is specied, the
default value is returned instead.
Raises
KeyError if key is not in dictionary and no default is specied
Examples
Delete entry with key 'hello' and print its value
my_dictionary = {'hello': 1, 'goodbye': 2}
hello_value = my_dictionary.pop('hello')
print(hello_value)
#1
print(my_dictionary)
# {'goodbye': 2}
https://jeffknupp.com/blog/2015/08/30/python-dictionaries/
9/13
6/26/2016
Python Dictionaries
d.popitem()
Pop (i.e. delete and return) a random element from the dictionary
Returns
A (key, value) tuple if d is not empty.
Raises
KeyError if d is empty. I personally think that's a stupid exception to raise
since no key was ever specied, but, hey, I didn't write the language.
Examples
Destructively iterate over values:
try:
key, value = d.popitem():
print 'Got {}: {}'.format(key, value)
except KeyError:
print 'Done'
d.setdefault(key[, default])
Get a key from the dictionary or, if it's not there, insert it with a default
value and return that. default, erm, defaults to None
Returns
d[key] if key is in d.
If not, do d[key] = default and then return d[key] (which will always
return default).
Raises
https://jeffknupp.com/blog/2015/08/30/python-dictionaries/
10/13
6/26/2016
Python Dictionaries
Raises
N/A
Examples
Count the number of times each word is seen in a le:
words = {}
for word in file:
occurrences = words.setdefault(word, 0)
words[word] = occurrences + 1
d.update(other)
Update a dictionary with the keys and values in other, overwriting existing
keys and values if there is any overlap.
Returns
None
Raises
N/A
Examples
Merge two dictionaries:
first = {'a': 1}
second = {'b': 2}
first.update(second)
print first
# {'a': 1, 'b': 2}
print second
# {'b': 2}
11/13
6/26/2016
Python Dictionaries
https://jeffknupp.com/blog/2015/08/30/python-dictionaries/
12/13
6/26/2016
Python Dictionaries
1 Comment
jeknupp.com
Share
Recommend 1
Login
Sort by Best
6 months ago
Reply Share
ALSO ON JEFFKNUPP.COM
Python Dictionaries
12 comments 10 months ago
https://jeffknupp.com/blog/2015/08/30/python-dictionaries/
13/13