10.6: Chapter Summary
- Page ID
- 117590
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\dsum}{\displaystyle\sum\limits} \)
\( \newcommand{\dint}{\displaystyle\int\limits} \)
\( \newcommand{\dlim}{\displaystyle\lim\limits} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\(\newcommand{\longvect}{\overrightarrow}\)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\ket}[1]{\left| #1 \right>}\)
\(\newcommand{\bra}[1]{\left< #1 \right|}\)
\(\newcommand{\braket}[2]{\left< #1 \vphantom{#2} \right| \left. #2 \vphantom{#1} \right>}\)
\(\newcommand{\braopket}[3]{\left< #1 \vphantom{#2}\vphantom{#3} \right| #2 \vphantom{#1}\vphantom{#3} \left| #3 \vphantom{#1}\vphantom{#2} \right>}\)
\(\newcommand{\qmvec}[1]{\mathbf{\vec{#1}}}\)
\(\newcommand{\op}[1]{\hat{\mathbf{#1}}}\)
\(\newcommand{\expect}[1]{\langle #1 \rangle}\)
\(\newcommand{\dfn}[1]{\emph{\textbf{#1}}}\)
Highlights from this chapter include:
- A dictionary in Python is a container object including key-value pairs.
- The
dicttype implements a dictionary in Python. - A dictionary cannot have duplicate keys.
- A dictionary is a mutable object but keys in the dictionary must be immutable objects.
- A dictionary can be created using curly braces or the
dict()method. - Values in the dictionary can be obtained through square bracket notation or the
get()method. - Dictionary items, keys, and values can be obtained using
items(),keys(), andvalues()methods, respectively. - Existing items can be modified or new items can be added to a dictionary using square brackets notation or the
update()method. - Items can be removed from a dictionary using the
delkeyword or thepop()method. - Conditional statements can be used with a dictionary to check if the dictionary contains specific keys, values, or key-value pairs.
- Looping on a dictionary can be done by iterating over keys, values, or items.
- Nested dictionaries are dictionaries that are stored as values within another dictionary.
- With dictionary comprehension, elements of an iterable object are transformed into key-value pairs.
At this point, you should be able to use dictionaries in your programs. The programming practice below ties together most topics presented in the chapter.
| Concept | Description |
|---|---|
| Dictionary creation using curly braces |
my_dict = {key1:value1, key2:value2}
|
|
Dictionary creation using the |
# Using a list
my_list = [(key1, value1), (key2, value2)]
my_dict = dict(my_list)
# Using keyword arguments
my_dict = dict(key1=value1, key2=value2)
# From another dictionary
old_dict = {key1: value1, key2: value2}
new_dict = dict(old_dict)
|
| Accessing dictionary items |
my_dict = {key1: value1, key2: value2}
# Accessing item using square bracket notation
my_dict[key1]
# Accessing item through get() method
my_dict.get(key1)
|
| Accessing all dictionary items |
my_dict.items() |
| Accessing all dictionary keys |
my_dict.keys() |
| Accessing all dictionary values |
my_dict.values() |
| Adding a new key-value pair or updating an existing key-value pair |
my_dict = {key1: value1, key2: value2}
# Updating an item using square bracket notation
my_dict[key1] = new_value
# Adding a new key-value pair using square bracket notation
my_dict[key3] = value3
# Updating an item using update() method
my_dict.update({key1: new_value})
# Adding a new key-value pair using update() method
my_dict.update({key3: value3})
|
| Deleting a key-value pair from a dictionary |
my_dict = {key1: value1, key2: value2}
# Using del keyword
del my_dict[key1]
# Using pop() method
deleted_value = my_dict.pop(key1)
|
| Iterating over a dictionary |
for key in dictionary: # Loop expression
# Statements to execute in the loop
#Statements to execute after the loop
|
| Nested dictionaries |
{
key_1:{key11:value11, key12:value12},
key_2:{key21:value21, key22:value22}
}
|
| Dictionary comprehension |
{key_expression: value_expression for element in iterable}
|
Given a list, create a dictionary with two keys, "even" and "odd". The values associated with each key must be the list of corresponding even and odd values in the given list.
Input:
input_list = [3, 5, 6, 1]
Prints {"even": [6], "odd":[3, 5, 1]}
[1]

