Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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
โ Incorrect
The keys() method returns all the keys in a dictionary.
2fill in blank
mediumComplete 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'
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
โ Incorrect
The values() method returns all the values in a dictionary.
3fill in blank
hardFix 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'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using keys() or values() which return only one part
Using get() which requires a key argument
โ Incorrect
The items() method returns all key-value pairs as tuples.
4fill in blank
hardFill 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'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using word instead of len(word) for length
Using less than symbol instead of greater than
โ Incorrect
Use len(word) to get the length of each word and filter words with length greater than 3 using >.
5fill in blank
hardFill 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'
Attempts:
3 left
๐ก Hint
Common Mistakes
Using wrong variable names like v instead of val or k
Not converting keys to uppercase
โ Incorrect
Use k.upper() to make keys uppercase, val for values, and k as the key variable in the loop.