Question: Please answer in Python (no imports/list comprehensions, only for loops and basic python functions) Write a function that takes a list of tuples (the output

Please answer in Python (no imports/list comprehensions, only for loops and basic python functions)

Write a function that takes a list of tuples (the output from the previous question, i.e. a list of (, ) tuples) and converts it to a dictionary, where keys are states and values are a list of nicknames associated with that state. Return an empty dictionary if no tuples are provided.

Notes: You can assume that there aren't duplicate nicknames. You can treat NO STATE as a state name and place that in the dictionary as well.

def dict_of_nicknames(tuples): """ >>> dict_of_nicknames([('AL', 'Cotton State'), ... ('LA', 'Pelican State'), ('LA', 'Creole State'), ... ('AL', 'Yellowhammer State'), ('LA', 'Sugar State'), ... ('MS', 'Magnolia State')]) {'AL': ['Cotton State', 'Yellowhammer State'], \ 'LA': ['Pelican State', 'Creole State', 'Sugar State'], \ 'MS': ['Magnolia State']}

>>> dict_of_nicknames([('MO', 'Show Me State'), ... ('NY', 'Empire State'), ('NO STATE', 'Lone Star State')]) {'MO': ['Show Me State'], 'NY': ['Empire State'], \ 'NO STATE': ['Lone Star State']} >>> dict_of_nicknames([('NO STATE', 'Granite State'), \ ('NO STATE', 'Sooner State')]) {'NO STATE': ['Granite State', 'Sooner State']} """

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!