0
0
Pythonprogramming~10 mins

Dictionary iteration in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print all keys in the dictionary.

Python
my_dict = {'a': 1, 'b': 2, 'c': 3}
for key in my_dict[1]:
    print(key)
Drag options to blanks, or click blank then click option'
A.keys()
B.items()
C.values()
D[]
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using .values() instead of .keys() will give values, not keys.
Using .items() returns key-value pairs, not just keys.
2fill in blank
medium

Complete the code to print all values in the dictionary.

Python
my_dict = {'x': 10, 'y': 20, 'z': 30}
for value in my_dict[1]:
    print(value)
Drag options to blanks, or click blank then click option'
A[]
B.values()
C.items()
D.keys()
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using .keys() will give keys, not values.
Using .items() returns key-value pairs, not just values.
3fill in blank
hard

Fix the error in the code to print keys and values.

Python
my_dict = {'name': 'Alice', 'age': 25}
for key, value in my_dict[1]:
    print(key, value)
Drag options to blanks, or click blank then click option'
A.keys()
B.values()
C[]
D.items()
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using .keys() or .values() will cause unpacking errors.
Not using any method will cause a runtime error.
4fill in blank
hard

Fill both blanks to create a dictionary of squares for even numbers only.

Python
numbers = [1, 2, 3, 4, 5]
squares = {num: num[1]2 for num in numbers if num [2] 2 == 0}
Drag options to blanks, or click blank then click option'
A**
B%
C//
D+
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using + instead of ** will add instead of square.
Using // instead of % will not correctly check even numbers.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase keys and values greater than 10.

Python
data = {'a': 5, 'b': 15, 'c': 20}
result = [1]: [2] for [3], val in data.items() if val > 10}
Drag options to blanks, or click blank then click option'
Ak.upper()
Bval
Ck
Dv
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using 'v' instead of 'val' causes undefined variable error.
Not using .upper() keeps keys lowercase.