Question
IN PYTHON. URGENTTTT Write a FUNCTION that accepts a dictionary and inverts it such that its keys become values and values become keys. For example,
IN PYTHON. URGENTTTT
Write a FUNCTION that accepts a dictionary and "inverts" it such that its keys become values and values become keys. For example, consider the following dictionary that represents grades earned by students in a class:
grades = {
"Pikachu":"A",
"Bulbasaur":"B",
"Squirtle":"A",
"Charmander":"C",
"Blastoise":"A"
}
An inverted form of this dictionary would look like the following:
inverted = {
'A': ['Pikachu', 'Squirtle', 'Blastoise'],
'B': ['Bulbasaur'],
'C': ['Charmander']
}
Note that in the inverted dictionary the values associated with each key are contained within a list, since in this example it is possible for more than one student to earn the same grade. Here are some considerations for this program:
You are writing a function which is accepting a single argument (a dictionary)
Your function should return the inverted form of the supplied dictionary (also a dictionary)
The inverted form of the dictionary will contain a series of keys which will be holding a series of lists these lists will contain the values associated with the key in question
You do not need to document your function using IPO notation
You cannot "hard-code" your solution to work only with these sample dictionaries
Here's are a few samples that show how your function should operate.
inverted1 = invert( {"apple":"fruit", "pear":"fruit", "carrot":"veg"} )
print (inverted1)
# {'fruit': ['apple', 'pear'], 'veg': ['carrot']}
inverted2 = invert( {"apple":"noun", "pear":"noun", "run": "verb", "quickly":"adjective", "carrot":"noun"} )
print (inverted2)
# {'noun':['apple','pear','carrot'],'verb':['run'],'adjective': ['quickly']
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started