

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 4
click for more info
Not enough gems
Cost: 6 gems
1: Welcome to Pandas
incomplete
2: Data Types
incomplete
3: Data Analytics Workflow
incomplete
4: Dates and Times
incomplete
5: Datetime Math
incomplete
6: Comparing Dates
incomplete
7: List Comprehensions
incomplete
8: Dictionary Comprehensions
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
If list comprehensions build lists, dictionary comprehensions build dictionaries. Say you have a list of game records:
games = [
{"game_id": "g-01", "title": "The Legend of Zelda", "status": "released"},
{"game_id": "g-02", "title": "Silksong", "status": "delayed"},
{"game_id": "g-03", "title": "Hades II", "status": "released"},
]
and want a dictionary where you can lookup game_id --> title. You can use a dictionary comprehension:
game_titles = {game["game_id"]: game["title"] for game in games}
print(game_titles)
# {'g-01': 'The Legend of Zelda', 'g-02': 'Silksong', 'g-03': 'Hades II'}
To filter records while creating the dictionary, you can optionally add an if condition:
released_titles = {
game["game_id"]: game["title"] for game in games if game["status"] == "released"
}
print(released_titles)
# {'g-01': 'The Legend of Zelda', 'g-03': 'Hades II'}
So, to be precise, the syntax is:
{key_expression: value_expression for item in iterable if condition}
Complete the map_active_models function. It accepts a list of device dictionaries and returns a new dictionary that maps each active device's device_id to its model name.