10.2: Dictionary Creation
- Page ID
- 117586
\( \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
- Create a dictionary object with given key/value pairs.
Dictionary creation
Two methods exist for creating an empty dictionary:
- Using curly braces
{}. Ex:dict_1 = {}. - Using the
dict()function. Ex:dict_2 = dict().
A dictionary object can also be created with initial key-value pairs enclosed in curly braces. Ex: my_dict = {"pizza": 2, "pasta": 3, "drink": 4} creates a dictionary object my_dict. A key and associated value are separated by a colon, and key-value pairs are separated by commas.
What is the correct syntax to create an empty dictionary in Python?
my_dict = ()my_dict = {}my_dict = []
- Answer
-
b. Curly braces can be used to create an empty dictionary.
What is the correct syntax to create a dictionary with one item
"one": 1?
my_dict = ("one": 1)my_dict = ("one", 1)my_dict = {"one": 1}
- Answer
-
c.
{"one": 1}creates a dictionary object with one item"one": 1.
How many items does the my_dict object contain?
my_dict = {"a": 1, "b": 2}
- 1
- 2
- 4
- Answer
-
b. Two pairs of items exist in the
my_dictdictionary. The two pairs are"a": 1and"b": 2.
dict() for dictionary creation
A dictionary object can be created with initial key-value pairs using the dict() function.
- Creating a dictionary from a list of tuples.
my_list = [("apple", 2), ("banana", 3), ("orange", 4)] my_dict = dict(my_list) - Creating a dictionary using keyword arguments.
my_dict = dict(apple=2, banana=3, orange=4) - Creating a dictionary from another dictionary.
old_dict = {"apple": 2, "banana": 3, "orange": 4} new_dict = dict(old_dict)
dict() for dictionary initializationWhat is the correct syntax to create a dictionary with initial values in Python?
my_dict = [key1:value1, key2:value2]my_dict = dict(key1=value1, key2=value2)my_dict = {key1=value1, key2=value2}
- Answer
-
b.
dict()function, along with keyword arguments, can be used to create a dictionary object.
What is the output of the following code?
my_dict = dict({"a": 1})
print(my_dict)
-
{"a": 1} -
<class, 'dict'>
-
"a" 1
- Answer
-
a.
my_dictis a dictionary with one key-value pair,"a": 1.
Which option creates a dictionary with two key-value pairs, "a": "A" and "b": "B"?
dict(a: A, b: B)dict([("a", "A"), ("b", "B")])dict({"a" = "A", "b" = "B"})
- Answer
-
b. The
dict()function can create a dictionary object from a list of tuples. The first element of each tuple acts as the key, and the second element is the value.
Create a dictionary, my_info, with three key-value pairs. The keys should be "first name", "last name", and "age" with the values being corresponding information about yourself. Then, print my_info.
- Answer
-
my_info = {"first name": "Parham", "last name": "Aarabi", "age": 46}print(my_info)


