0
0
Pythonprogramming~10 mins

Removing dictionary entries 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 remove the key 'b' from the dictionary.

Python
my_dict = {'a': 1, 'b': 2, 'c': 3}
my_dict.[1]('b')
print(my_dict)
Drag options to blanks, or click blank then click option'
Apop
Bremove
Cdelete
Ddiscard
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using list methods like remove() which do not exist for dictionaries.
Trying to use delete which is not a dictionary method.
2fill in blank
medium

Complete the code to remove the key 'x' safely without error if it does not exist.

Python
data = {'x': 10, 'y': 20}
removed = data.[1]('x', None)
print(data, removed)
Drag options to blanks, or click blank then click option'
Aremove
Bdelete
Cdiscard
Dpop
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using remove() which is not a dictionary method.
Using del statement which raises error if key missing.
3fill in blank
hard

Fix the error in the code to remove the key 'k' from the dictionary.

Python
d = {'k': 5, 'm': 7}
d.[1]('k')
print(d)
Drag options to blanks, or click blank then click option'
Aremove
Bpop
Cdiscard
Ddelete
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using remove() which is for lists, not dictionaries.
Trying to use delete which is not a method.
4fill in blank
hard

Fill both blanks to remove all keys with values less than 5 from the dictionary.

Python
d = {'a': 3, 'b': 7, 'c': 2}
for key in list(d.[1]()):
    if d[key] [2] 5:
        d.pop(key)
print(d)
Drag options to blanks, or click blank then click option'
Akeys
B>
C<
Ditems
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using items() without unpacking keys and values.
Using greater than operator instead of less than.
5fill in blank
hard

Fill all three blanks to create a new dictionary excluding entries with value 0.

Python
original = {'x': 0, 'y': 5, 'z': 0}
filtered = { [1]: [2] for [3] in original.items() if [2] != 0 }
print(filtered)
Drag options to blanks, or click blank then click option'
Ak
Bv
Ck, v
Dkey
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using only one variable to unpack items.
Using wrong variable names inconsistently.