Practice · Python

Practice Dict & Set Comprehensions

Construct dicts and sets with comprehensions to build lookups and unique collections concisely.

Dict & Set Comprehensionswarming python…

Music Word Statistics

Write a function music_word_stats(words) that receives a list of word strings (e.g. words from song lyrics). It must return a three‑element tuple:

  1. A dictionary mapping each word to its length.
  2. An inverted dictionary that maps each length to a set of all words (in their original case) that have that length.
  3. A set containing all unique lower‑cased words from the input.

For example:

music_word_stats(["Love", "baby", "Love", "Rock"]) == (
    {"Love": 4, "baby": 4, "Rock": 4},
    {4: {"Love", "baby", "Rock"}},
    {"love", "baby", "rock"}
)
Press Run to see output, or Check to grade against the tests.

Want a whole course built around dict & set comprehensions?

Start my path — free

Adaptive practice, streaks, projects with real code review. Free tier gates AI help, never content.