Computer Science Grade 9 20 min

Set and Dictionary Comprehensions: Expanding Comprehension Power

Learn to use set and dictionary comprehensions for creating sets and dictionaries concisely.

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Define and differentiate between a set and a dictionary comprehension. Write a set comprehension to create a set from an existing iterable. Write a dictionary comprehension to create a dictionary from an existing iterable. Incorporate conditional logic (if statements) into set and dictionary comprehensions. Convert a simple for loop that creates a set or dictionary into its equivalent comprehension. Identify the correct syntax for creating set and dictionary comprehensions. Ever wanted to clean up a list of duplicate names or instantly create a scorecard from a list of players and scores in a single line of code? 🧙‍♂️ Let's learn how! You've seen how list comprehensions can build lists quickly. Now, we'll expand that power to two other im...
2

Key Concepts & Vocabulary

TermDefinitionExample ComprehensionA compact way of creating a new data structure (like a list, set, or dictionary) from an existing one. It's like a one-line for loop.List Comprehension: `squares = [x*x for x in range(5)]` results in `[0, 1, 4, 9, 16]`. SetAn unordered collection of unique items. Duplicates are automatically removed.The list `[1, 2, 2, 3]` becomes the set `{1, 2, 3}`. DictionaryA collection of key-value pairs. Each key must be unique and is used to look up its corresponding value.`student = {'name': 'Alex', 'grade': 9}`. Here, 'name' is a key and 'Alex' is its value. Set ComprehensionA one-line expression for creating a new set. It uses curly braces `{}` and does not have key-value pairs.`unique_squares = {x*x for x...
3

Core Syntax & Patterns

Set Comprehension Syntax {expression for item in iterable if condition} Use this to create a set from an iterable. The `expression` is what goes into the set for each `item`. The `if condition` is optional and filters which items are included. Dictionary Comprehension Syntax {key_expression: value_expression for item in iterable if condition} Use this to create a dictionary. For each `item` in the `iterable`, a `key_expression` and a `value_expression` are calculated to form a key-value pair. The `if condition` is also optional.

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
Given a list of tuples `pairs = [('a', 1), ('b', 2), ('c', 1)]`, which dictionary comprehension correctly creates a dictionary from these pairs?
A.{p for p in pairs}
B.{p[0]: p[1] for p in pairs}
C.{key: value for pairs}
D.{p.key: p.value for p in pairs}
Challenging
You want to invert a dictionary, swapping keys and values. Given `d = {'a': 1, 'b': 2}`, which comprehension achieves this?
A.{key: value for key, value in d}
B.{d[key]: d[value] for key, value in d.items()}
C.{value: key for key, value in d.items()}
D.{key: value for value, key in d.items()}
Challenging
Given `data = [{'name': 'A', 'val': 10}, {'name': 'B', 'val': 20}, {'name': 'C', 'val': 5}]`, which set comprehension creates a set of the `name`s for all entries where `val` is greater than or equal to 10?
A.{d for d in data if d['val'] >= 10}
B.{d.name for d in data if d.val >= 10}
C.{d['name'] for d in data if d['val'] >= 10}
D.{d['name'] if d['val'] >= 10 for d in data}

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Advanced Python Techniques: Beyond the Basics

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.