1. Introduction to Declarative Data Constructs
In previous lessons, we covered all fundamental Python data structures: Lists (`list`), Tuples (`tuple`), Sets (`set`), and Dictionaries (`dict`). When manipulating or transforming these structures using traditional imperative loops (e.g., initializing an empty list, looping over an existing collection, applying conditional logic, and calling .append() or .add()), code often becomes verbose and repetitive.
Python solves this by providing one of its most celebrated syntactic features: Comprehensions. Comprehensions offer a compact, elegant, and highly performant declarative syntax for creating new data structures from existing iterables in a single line of clear, Pythonic code.
2. List Comprehensions (`[expression for item in iterable]`)
1. Core Syntax and Execution Mechanics
A List Comprehension transforms an iterable into a new list by applying an output expression to each element, optionally filtered by a Boolean condition.
# General Formula for List Comprehension with Optional Filtering:
new_list = [output_expression for item in iterable if conditional_predicate]
This single line replaces the traditional multi-line imperative loop pattern:
# Equivalent Imperative Loop Pattern:
new_list = []
for item in iterable:
if conditional_predicate:
new_list.append(output_expression)
.append()) and stack lookups during loop iterations.
raw_prices = [10.0, 25.50, 40.0, 80.0, 100.0]
# 1. Simple Transformation: Applying 18% Tax to all items
taxed_prices = [round(price * 1.18, 2) for price in raw_prices]
# 2. Filtering + Transformation: Applying tax ONLY to items >= $40.0
expensive_taxed = [round(price * 1.18, 2) for price in raw_prices if price >= 40.0]
print("Original Prices :", raw_prices)
print("All Taxed Prices:", taxed_prices)
print("Filtered Taxed :", expensive_taxed)
Original Prices : [10.0, 25.5, 40.0, 80.0, 100.0] All Taxed Prices: [11.8, 30.09, 47.2, 94.4, 118.0] Filtered Taxed : [47.2, 94.4, 118.0]
2. Conditional Value Branching (`if-else` in Output Expression)
When you need to **filter** elements, place if at the *end* of the comprehension. However, when you need to **transform** values dynamically based on a condition (like a ternary operator), place if-else in the *front* output expression:
[expression_if_true if condition else expression_if_false for item in iterable]
scores = [85, 42, 91, 55, 68]
# Using inline ternary operator inside output expression
pass_fail_labels = ["PASS" if score >= 60 else "FAIL" for score in scores]
print("Student Scores:", scores)
print("Status Labels :", pass_fail_labels)
Student Scores: [85, 42, 91, 55, 68] Status Labels : ['PASS', 'FAIL', 'PASS', 'FAIL', 'PASS']
3. Set Comprehensions (`{expression for item in iterable}`)
A Set Comprehension shares identical syntax with list comprehensions, but uses curly braces {} instead of square brackets []. It builds a unique, unordered Set (`set`), automatically eliminating duplicate results upon creation.
raw_tags = [" python ", " JAVA ", "python", " Docker ", "java ", "SQL "]
# Normalizing strings and automatically deduplicating into a unique Set
cleaned_tags = {tag.strip().title() for tag in raw_tags}
print("Raw Tag Inputs :", raw_tags)
print("Unique Set Tags:", cleaned_tags)
print("Resulting Type :", type(cleaned_tags))
Raw Tag Inputs : [' python ', ' JAVA ', 'python', ' Docker ', 'java ', 'SQL ']
Unique Set Tags: {'Docker', 'Python', 'Sql', 'Java'}
Resulting Type :
4. Dictionary Comprehensions (`{key_expr: val_expr for item in iterable}`)
A Dictionary Comprehension constructs a new Dictionary (`dict`) dynamically by specifying a key_expression: value_expression pair evaluated per iteration item.
products = ["Laptop", "Mouse", "Keyboard"]
prices = [1200.0, 25.0, 75.0]
# 1. Zip mapping list pairs into a Dictionary using comprehension
catalog = {item: price for item, price in zip(products, prices)}
# 2. Transforming existing Dictionary (Applying 10% Discount)
discounted_catalog = {k: v * 0.90 for k, v in catalog.items() if v > 50.0}
print("Catalog Dict :", catalog)
print("Discounted Dict:", discounted_catalog)
Catalog Dict : {'Laptop': 1200.0, 'Mouse': 25.0, 'Keyboard': 75.0}
Discounted Dict: {'Laptop': 1080.0, 'Keyboard': 67.5}
5. Nested Comprehensions and Flattening Data
Comprehensions can include nested loops to flatten multidimensional matrices or process multi-level data structures.
for loops! Avoid nesting more than 2 levels deep, as complex nested comprehensions violate PEP 8 readability principles.
# 2D Matrix Grid
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
# Flattening 2D Matrix into a 1D List
# Outer loop (for row in matrix) comes FIRST, followed by Inner loop (for num in row)
flattened = [num for row in matrix for num in row]
# Extracting only even numbers from 2D Matrix
even_numbers = [num for row in matrix for num in row if num % 2 == 0]
print("Original 2D Matrix:", matrix)
print("Flattened 1D List :", flattened)
print("Filtered Even List:", even_numbers)
Original 2D Matrix: [[1, 2, 3], [4, 5, 6], [7, 8, 9]] Flattened 1D List : [1, 2, 3, 4, 5, 6, 7, 8, 9] Filtered Even List: [2, 4, 6, 8]
6. Integrating Comprehensions with `match-case` Pattern Matching
Combining list/dict comprehensions with match-case structural pattern matching creates elegant data processing pipelines that validate, transform, and route multi-schema payload collections in very few lines of code.
def process_batch_transactions(batch_payload: list):
"""
Parses and transforms transactional payload lists using match-case and comprehensions.
Demonstrates processing dynamic data streams cleanly.
"""
match batch_payload:
case ["BATCH_TAX", rate, *items] if isinstance(rate, float):
# Using List Comprehension to apply tax rate to dynamic items
processed = [round(item * (1 + rate), 2) for item in items if isinstance(item, (int, float))]
return f"BATCH TAX APPLIED (+{rate:.0%}): {processed}"
case ["BATCH_FILTER", min_val, *items]:
# Using Set Comprehension to deduplicate items above threshold
valid_set = {item for item in items if isinstance(item, (int, float)) and item >= min_val}
return f"UNIQUE ITEMS (>= {min_val}): {valid_set}"
case ["MAP_ROLES", *user_tuples]:
# Using Dictionary Comprehension to map tuples into key-value profile dict
role_dict = {uid: role.upper() for uid, role in user_tuples if len(uid) > 0}
return f"MAPPED ROLE DICTIONARY: {role_dict}"
case _:
return "ERROR: Unrecognized batch pipeline schema."
# Testing the pipeline dispatcher
print(process_batch_transactions(["BATCH_TAX", 0.18, 100.0, 200.0, 50.0]))
print(process_batch_transactions(["BATCH_FILTER", 50, 100, 20, 100, 75, 50]))
print(process_batch_transactions(["MAP_ROLES", ("usr_1", "admin"), ("usr_2", "editor")]))
BATCH TAX APPLIED (+18%): [118.0, 236.0, 59.0]
UNIQUE ITEMS (>= 50): {100, 75, 50}
MAPPED ROLE DICTIONARY: {'usr_1': 'ADMIN', 'usr_2': 'EDITOR'}
7. Frequently Asked Interview Questions with Answers
for loop constructs.
- Filtering conditions belong at the **end** of the comprehension:
[x for x in data if x > 0]. - Ternary transformation expressions belong in the **front** output position:
[x if x > 0 else 0 for x in data].
[] and evaluates eagerly, creating a full list object immediately in memory. A Generator Expression uses parentheses () and evaluates lazily, returning an iterator that yields one item at a time on demand with constant $O(1)$ memory consumption.
for loops. For example, [x for sublist in matrix for x in sublist] evaluates the outer loop for sublist in matrix first, followed by the inner loop for x in sublist.
{expr for x in data} discard duplicates automatically as elements are evaluated. Dictionary comprehensions {k: v for x in data} evaluate key-value pairs, where duplicate keys overwrite previous key entries in-place.
8. Homework & Practical Assignments
Task 1: List and Set Comprehension Data Cleaner
Create a script named comprehension_cleaner.py inside your lesson_25 folder:
- Define a dirty dataset list:
raw_data = [" apple ", " BANANA ", "123", " ", "cherry ", " BANANA ", " "]. - Use a List Comprehension with an
iffilter to remove whitespace-only items, clean strings using.strip(), and convert to title case. - Use a Set Comprehension to construct a unique set of clean fruit strings directly.
- Print both outputs using f-strings.
Task 2: Dictionary Comprehension Price Catalog Mapper
Create a script named dict_comp_catalog.py:
- Define a product dictionary:
prices = {"Laptop": 1200.0, "Mouse": 25.0, "Monitor": 300.0, "Keyboard": 75.0}. - Use a Dictionary Comprehension to apply a 15% discount to all products priced above $50.0, rounding prices to 2 decimal places.
- Use another Dictionary Comprehension to invert the dictionary mapping (mapping prices to product names).
- Print both resulting dictionaries.
Task 3: Master Capstone Project — High-Performance Data Processing OS (`data_pipeline_os.py`)
Create a script named data_pipeline_os.py inside your lesson_25 folder. This task tests and integrates **ALL concepts learned across Lessons 1 through 25**.
Project Architectural Requirements Specification:
- Environment, Modules & Entry Guard (Lessons 18-20):
- Import standard library modules (
sys,datetime,json). - Wrap main execution inside an
if __name__ == "__main__":guard block.
- Import standard library modules (
- Comprehensions Across Data Structures (Lesson 25):
- Use List Comprehensions to filter, transform, and flatten multidimensional record arrays.
- Use Set Comprehensions to extract unique user tag collections.
- Use Dictionary Comprehensions to construct fast lookup tables from sequence pairs.
- Core Data Structure Integration (Lessons 21-24):
- Store catalog inventory inside
dictmapping containers. - Maintain audit history logs using NamedTuple objects inside a
list. - Use
setalgebra for system permission verifications.
- Store catalog inventory inside
- Advanced Function Parameters & Scope (Lessons 15-17):
- Structure pipeline functions with type hints, docstrings,
*args/**kwargslogging, and early return Guard Clauses. - Incorporate functional tools (
sorted()with custom tuple lambda keys).
- Structure pipeline functions with type hints, docstrings,
- Indefinite & Definite Loops (Lessons 13-14):
- Run the main application inside a continuous
while Trueinteractive menu loop. - Iterate through record streams using
forloops withenumerate()and.items().
- Run the main application inside a continuous
- Pattern Matching CLI Dispatcher (Lesson 12):
- Process user CLI command tokens inside a
match-caseblock:case ["LOAD", *raw_vals]→ Process strings using list comprehensions and update dataset.case ["TRANSFORM", "TAX", rate_str]→ Apply tax using list comprehensions with conditional pattern guards.case ["FILTER", "UNIQUE"]→ Deduplicate records using set comprehensions.case ["CATALOG", "BUILD"]→ Construct lookup dict using dictionary comprehensions.case ["EXIT" | "QUIT"]→ Terminate session safely using a sentinel flag.case _→ Output command error message.
- Process user CLI command tokens inside a
- Conditionals, Formatting & Foundations (Lessons 1-11):
- Sanitize all inputs using
.strip()and.upper(). - Format tabular reports using f-strings with field width alignment specifiers (
:<15,:>10) and currency formatting (:,.2f).
- Sanitize all inputs using
9. File & Workspace Directory Structure
Standard Course Workspace Layout:
Ensure all exercise files are stored inside their corresponding lesson directories:
python_mastery_course/
│
├── lesson_01/ ... lesson_24/
│
└── lesson_25/
├── list_comprehension_basic.py
├── list_comprehension_branching.py
├── set_comprehension_demo.py
├── dict_comprehension_demo.py
├── nested_comprehension_demo.py
├── match_case_comprehension_pipeline.py
├── comprehension_cleaner.py <-- (Task 1)
├── dict_comp_catalog.py <-- (Task 2)
└── data_pipeline_os.py <-- (Task 3: Master Review Capstone)
OnlineCBT