Complete the code to create an empty dictionary for the inverted result.
inverted = [1]We use {} to create an empty dictionary in Python.
Complete the code to loop through the original dictionary's items.
for key, value in original.[1](): inverted[value] = key
keys() only gives keys, not values.values() only gives values, not keys.The items() method returns key-value pairs from a dictionary.
Fix the error in the code to invert the dictionary correctly.
inverted = {}
for k, v in data.items():
inverted[[1]] = kk as the new key keeps the dictionary the same.data or inverted as keys causes errors.When inverting, the original value v becomes the new key.
Fill the four blanks to create a dictionary comprehension that inverts the dictionary.
inverted = { [1]: [2] for [3], [4] in original.items() }In a dictionary comprehension, the syntax is {new_key: new_value for key, value in dict.items()}. Here, v is the new key and k is the new value.
Fill all three blanks to invert the dictionary and handle duplicate values by storing keys in a list.
inverted = {}
for [1], [2] in original.items():
inverted.setdefault([3], []).append([1])setdefault to handle duplicates.We use setdefault with the value v as key and append the original key k to handle duplicates.