Answer to Question #196200 in Python for FORTUNE AIDOO

Question #196200

Design the dictionary so that it could be useful for something meaningful to you. Create at least three different items in it.

def invert_dict(d):

   inverse = dict()

   for key in d:

      val = d[key]

     if val not in inverse:

        inverse[val] = [key]

     else:

        inverse[val].append(key)

   return inverse 

Modify this function so that it can invert your dictionary. In particular, the function will need to turn each of the list items into separate keys in the inverted dictionary. 

Run your modified invert_dict function on your dictionary. Print the original dictionary and the inverted one. 

Include your output 

Describe what is useful about your dictionary. Then describe whether the inverted dictionary is useful or meaningful, and why.


1
Expert's answer
2021-05-20T18:21:57-0400
def invert(d):
  inverse = dict()
  for key in d:
    val = d[key]
    for item in val:
      if item not in inverse:
        inverse[item] = [key]
      else:
        inverse[item].append(key)
  return inverse 


"""name : [animal type, age, sex]"""

animals = {
  "Teddy": ["dog",4,"male"],
  "Elvis": ["dog",1,"male"],
  "Sheyla": ["dog",5,"female"],
  "Topic": ["hamster",3,"male"],
  "Kuzya": ["cat",10,"male"],
  "Misi": ["cat",8,"female"]
}

if __name__ == "__main__":
    print(animals, end="\n");
    print(invert(animals), end="\n");

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS