0
0
Pythonprogramming~10 mins

Dictionary keys, values, and items 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 get all keys from the dictionary.

Python
my_dict = {'a': 1, 'b': 2, 'c': 3}
keys = my_dict.[1]()
print(keys)
Drag options to blanks, or click blank then click option'
Avalues
Bkeys
Citems
Dget
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using values() instead of keys()
Using items() which returns key-value pairs
Trying to access keys as an attribute without parentheses
2fill in blank
medium

Complete the code to get all values from the dictionary.

Python
my_dict = {'x': 10, 'y': 20, 'z': 30}
vals = my_dict.[1]()
print(vals)
Drag options to blanks, or click blank then click option'
Avalues
Bitems
Cpop
Dkeys
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using keys() instead of values()
Using items() which returns key-value pairs
Trying to pop a value without a key
3fill in blank
hard

Fix the error in the code to get all key-value pairs from the dictionary.

Python
data = {'name': 'Alice', 'age': 25}
pairs = data.[1]()
print(pairs)
Drag options to blanks, or click blank then click option'
Akeys
Bvalues
Cget
Ditems
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using keys() or values() which return only one part
Using get() which requires a key argument
4fill in blank
hard

Fill both blanks to create a dictionary of word lengths for words longer than 3 letters.

Python
words = ['apple', 'bat', 'carrot', 'dog']
lengths = {word: [1](word) for word in words if len(word) [2] 3}
print(lengths)
Drag options to blanks, or click blank then click option'
Alen
B>
C<
Dupper()
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using word instead of len(word) for length
Using less than symbol instead of greater than
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': 25}
filtered = [1]: [2] for [3], val in data.items() if val > 10}
print(filtered)
Drag options to blanks, or click blank then click option'
Ak.upper()
Bval
Ck
Dv
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using wrong variable names like v instead of val or k
Not converting keys to uppercase