logo CodeStepByStep logo

dict_comprehensions

Language/Type: Python collections list dict

Given the following list and dictionary, write list or dictionary comprehensions that would produce the following new collections:

# index     0        1      2      3
words = ["apple", "ball", "car", "dog"]
synonyms = {"hirsute" : "hairy",
            "erudite" : "learned",
            "abattoir": "slaughterhouse",
            "kwyjibo" : "ape"}

If you are processing the words list, write a dictionary comprehension in this format: {key: value for s in words}

If you are processing the synonyms dictionary, write a list comprehension in this format: [expr for k, v in synonyms.items()] Write a dictionary comprehension in this format: {key: value for k, v in synonyms.items()}

{'a': 'apple', 'b': 'ball', 'c': 'car', 'd': 'dog'}
{'ape': 'kwyjibo', 'hairy': 'hirsute', 'learned': 'erudite', 'slaughterhouse': 'abattoir'}
['abattoir means slaughterhouse', 'erudite means learned', 'hirsute means hairy', 'kwyjibo means ape']

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.