4. Data Structures: Dictionaries
Dictionaries
The last, but not least complex datatype Python provides is the #\mathtt{\color{#4271ae}{\text{dict}}}#, short for dictionary. A dictionary consists of a collection of key-value pairs. Like lists, dictionaries are mutable. However, unlike lists, elements are accessed via their respective keys, not via indexing. A #\mathtt{\color{#4271ae}{\text{dict}}}# can be defined using curly brackets and colons, or through the use of the #\mathtt{\color{#4271ae}{\text{dict}}\text{()}}# function.
#\mathtt{\{}\textit{key}_\textit{0}\mathtt{:}\textit{value}_\textit{0}, \textit{key}_\textit{1}\mathtt{:}\textit{value}_\textit{1},\dots, \textit{key}_\textit{n}\mathtt{:}\textit{value}_\textit{n}\mathtt{\}}#
#\color{#4271ae} {\mathtt{dict}}\mathtt{(}\textit{key}_\textit{0}\mathtt{=}\textit{value}_\textit{0}, \textit{key}_\textit{1}\mathtt{=}\textit{value}_\textit{1},\dots, \textit{key}_\textit{n}\mathtt{=}\textit{value}_\textit{n}\mathtt{)}#
#\color{#4271ae} {\mathtt{dict}}\mathtt{(}\mathtt{\text{[(}}\textit{key}_\textit{0},\textit{value}_\textit{0}\mathtt{\text{)}},\mathtt{\text{(}}\textit{key}_\textit{1},\textit{value}_\textit{1}\mathtt{\text{)}},\dots,\mathtt{\text{(}}\textit{key}_\textit{n},\textit{value}_\textit{n}\mathtt{\text{)]}}\mathtt{)}#
Where key must be an immutable object and value can be any valid Python object, including mutables. A dictionary has no particular order, eventhough its contents are displayed in the order they were defined when printed.
Like the other objects discussed, dictionaries also have their common syntax and specific methods. First, we will take a look at how to access and add elements.
The syntax for accessing dictionary elements is quite similar to that of lists, the same notation, where the identifier is followed by square brackets, is used. However, instead of using the position of an element, its key is referenced between the brackets.
>>> d = {"one": 1, "two": 2, "three": 3}
>>> d["two"] |
2
|
The same syntax can be used to either assign a new value to the key or when a new unique key is specified, to add a new key-value pair to the dictionary.
>>> d["two"] = 5
>>> d |
{'one': 1, 'two': 5, 'three': 3}
|
>>> d["four"] = 4
>>> d |
{'one': 1, 'two': 5, 'three': 3, 'four': 4}
|
To delete an item from the dictionary, you can use the #\mathtt{\color{#8959A8}{\text{del}}}# keyword in combination with the same syntax.
>>> del d["one"]
>>> d |
{'two': 5, 'three': 3, 'four': 4}
|
Next, we will take a look at the methods provided by the #\mathtt{\color{#4271ae}{\text{dict}}}# type.
In addition to the syntax provided for accesing elements, you can also access elements using the safer #\mathtt{\color{#4271ae}{\text{get}}\text{()}}# method. #\mathtt{\color{#4271ae}{\text{get}}\text{(}\textit{key}\text{)}}# searches the dictionary for the key and returns the associated value if it is found, it returns #\mathtt{\color{#F5871F}{\text{None}}}# otherwise.
>>> d = {"a": 1, "b": 2, "c": 3, "d": 4}
>>> d.get("a") |
1
|
>>> d.get("e")
|
None
|
The #\mathtt{\color{#4271ae}{\text{get}}\text{()}}# method is considered safer than directly indexing a dictionary because directly indexing a dictionary with a key that is not actually present in said dictionary results in an error.
>>> d["e"]
|
KeyError: 'e'
|
Another method that can be used to obtain elements is the #\mathtt{\color{#4271ae}{\text{pop}}\text{()}}# method. #\mathtt{\color{#4271ae}{\text{pop}}\text{(}\textit{key}\text{)}}# returns the value corresponding to key if the key is present in the dictionary. If succesful, #\mathtt{\color{#4271ae}{\text{pop}}\text{()}}# also removes the key-value pair.
>>> d.pop("d")
|
4
|
>>> d
|
{'a': 1, 'b': 2, 'c': 3}
|
The final method that can be used to obtain a single item is the #\mathtt{\color{#4271ae}{\text{popitem}}\text{()}}# method. #\mathtt{\color{#4271ae}{\text{popitem}}\text{()}}# returns the last key-value pair added to the dictionary as a #\mathtt{\color{#4271ae}{\text{tuple}}}#.
>>> d.popitem()
|
('c', 3)
|
Or visit omptest.org if jou are taking an OMPT exam.