Exercises
Dict and Set Comprehensions
3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.
Exercise 1Passed
Build a dictionary mapping each word to its length.
Python
words = ["ada", "grace"]
# print {'ada': 3, 'grace': 5}
A dict comprehension needs a key: value pair.
words = ["ada", "grace"]
print({word: len(word) for word in words})Exercise 2Passed
Keep only the entries scoring above 5.
Python
scores = {"ada": 9, "grace": 4, "kat": 7}
# print only the high scores
Walk .items() and put the condition at the end.
scores = {"ada": 9, "grace": 4, "kat": 7}
print({name: score for name, score in scores.items() if score > 5})Exercise 3Passed
Print the unique lowercase words, sorted.
Python
words = ["Ada", "ada", "GRACE"]
# print ['ada', 'grace']
A set comprehension drops duplicates; sort before printing.
words = ["Ada", "ada", "GRACE"]
print(sorted({word.lower() for word in words}))