Module 11
Comprehension Techniques
Learn concise data transformation using list, set, dictionary, and tuple comprehensions.
01
Python Comprehensions — List, Set, Dict, and Generator Overview
Write elegant one-line list, set, and dict comprehensions, split delimited text into collections, and see why parentheses create a generator.
02
List Comprehension — Expression, split(), and if Filters
Build Python lists in one line: cast tokens with split, double values, and keep positives with an if clause in a list comprehension.
03
Set Comprehension — Unique Values from Comma-Separated Tokens
Build a Python set in one line with curly braces, split comma-separated tokens, cast to int, and drop duplicates automatically.
04
Dict Comprehension — Squares and Word Lengths
Build Python dictionaries in one line: map each number to its square and each word to its length, then loop items or unpack in a single expression.
05
Tuple Comprehension? — Generators and tuple()
Parentheses around a for clause create a Python generator, not a tuple. Inspect the type, then convert with tuple() to get a real tuple.