Respuesta :
Answer:
def invert_dict(d):
# Create a dictionary to store the inverse.
inverse = dict()
# Start the loop to traverse the dictionary.
for key in d.keys():
# Store the list of values stored at the current key.
values = d[key]
# Start the loop to traverse the list of values.
for value in values:
# Create a new key if the value is not
# in the inverted dictionary.
if value not in inverse:
inverse[value] = [key]
# Otherwise, append the value to the current key.
else:
inverse[value].append(key)
# Return the inverted dictionary.
return inverse
# Define the main() function.
def main():
dic = {'a': ["aegon", "archangel"], 'b': ["blackbolt"]}
# Display the original dictionary.
print("Original dictionary:")
print(dic)
# Call the method and display the inverted dictionary.
print("\nInverted dictionary:")
print(invert_dict(dic))
# Call the main() function.
if __name__ == "__main__":
main()
Modification Part:
The inverted dictionary is not useful since the basic idea of a dictionary is to map key-value pairs. All the common attributes of the different values are mapped to the key. Ideally, it is assumed that value is mapped to only one key.
The inverted dictionary will have inverted key-value pairs. As seen above, the value is the same for all the keys that are made out of the list of values from the original key-value pair. For example, the keys ‘aegon’ and ‘archangel’ contains the same value ‘a’ since they are created from the list of values of the same key.
The keys of the inverted dictionary are no longer useful as most of the keys will have the same value if there is a large list mapped to a single key in the original dictionary.
Explanation: