10.1: Dictionary Basics
- Page ID
- 117585
\( \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}}}\)
By the end of this section you should be able to
- Describe the structure of a dictionary object.
- Use
type()to identify the type of a dictionary object. - Explain why a dictionary object does not have duplicate key values.
Dictionaries
A dictionary in Python is a container object including key-value pairs. An example of a key-value item is a word in an English dictionary with its corresponding definition.
Given the dictionary object days = {"Sunday": 1, "Monday": 2, "Tuesday": 3}, what are the keys in the days dictionary object?
"Sunday", "Monday", "Tuesday"1, 2, 30, 1, 2
- Answer
-
a. The first item in each key-value pair is the key. Hence
"Sunday", "Monday", "Tuesday"are key items.
What are the values in the days dictionary object?
0, 1, 21, 2, 3"Sunday", "Monday", "Tuesday"
- Answer
-
b. The second item in each key-value pair is the value. Hence
1, 2, 3are value items.
What is the value associated with key "Sunday"?
"Sunday"10
- Answer
-
b. Based on the
daysdictionary object, the value assigned to"Sunday"is1.
Dictionary type and properties
The dict type implements a dictionary in Python. Since a dictionary object is used to look up values using keys, a dictionary object cannot hold duplicate key values.
Given the dictionary object encoding = {"a": 97, "b": 98, "A": 65, "B": 66}, answer the following questions.
Given the dictionary object encoding = {"a": 97, "b": 98, "A": 65, "B": 66}, answer the following questions.
type(encoding) return?
dictionarydictstr
- Answer
-
b. The type of a dictionary object is
dict.
The encoding dictionary contains duplicate keys since it has both "a" and "A" as keys.
- true
- false
- Answer
-
b. A dictionary cannot have duplicate keys, and
"a"and"A"are two different keys because"a"and"A"are different characters.
A dictionary can have duplicate values.
- true
- false
- Answer
-
a. A dictionary can have duplicate values, but not duplicate keys.
Given the variables in the code below, examine the variables' types.
- Answer
-
var_1 = ["a1", "b2", "c3"]var_2 = {"a": 1, "b": 2, "c": 3}print(type(var_1))print(type(var_2))


