Logo OnlineCBT
PYTHON Tutorial
PYTHON · LESSON 25

List, Set and Dictionary Comprehensions

Build transformed collections with readable comprehensions.

Level: Beginner to Advanced
Duration: ~60 Mins Deep-Dive
Updated: 26 Jul 2026

Lesson 25: List, Set and Dictionary Comprehensions

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)
    
Performance Advantage: List comprehensions run at C-speed in CPython because bytecode execution skips repetitive Python-level method calls (like .append()) and stack lookups during loop iterations.
LIST_COMPREHENSION_BASIC.PY
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)
OUTPUT
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]
LIST_COMPREHENSION_BRANCHING.PY
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)
OUTPUT
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.

SET_COMPREHENSION_DEMO.PY
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))
OUTPUT
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.

DICT_COMPREHENSION_DEMO.PY
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)
OUTPUT
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.

Readability Warning: The loop ordering in a nested comprehension follows the exact same visual left-to-right order as standard nested for loops! Avoid nesting more than 2 levels deep, as complex nested comprehensions violate PEP 8 readability principles.
NESTED_COMPREHENSION_DEMO.PY
# 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)
OUTPUT
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.

MATCH_CASE_COMPREHENSION_PIPELINE.PY
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")]))
OUTPUT
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

Q1: What are Comprehensions in Python, and what are their primary advantages?
Answer: Comprehensions are declarative single-line expressions used to construct Lists, Sets, or Dictionaries from existing iterables. Their primary advantages include improved code conciseness, enhanced readability, and superior execution performance (C-speed bytecode execution) compared to traditional imperative for loop constructs.
Q2: Where should filtering `if` conditions vs `if-else` transformation expressions be placed in a comprehension?
Answer:
  • 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].
Q3: How does a Generator Expression `(x for x in data)` differ from a List Comprehension `[x for x in data]`?
Answer: A List Comprehension uses square brackets [] 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.
Q4: What is the loop execution order in a nested List Comprehension?
Answer: The loop order in a nested comprehension follows the exact same left-to-right order as standard nested 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.
Q5: How do Set and Dictionary Comprehensions enforce uniqueness and mapping rules?
Answer: Set comprehensions {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 if filter 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:

  1. Environment, Modules & Entry Guard (Lessons 18-20):
    • Import standard library modules (sys, datetime, json).
    • Wrap main execution inside an if __name__ == "__main__": guard block.
  2. 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.
  3. Core Data Structure Integration (Lessons 21-24):
    • Store catalog inventory inside dict mapping containers.
    • Maintain audit history logs using NamedTuple objects inside a list.
    • Use set algebra for system permission verifications.
  4. Advanced Function Parameters & Scope (Lessons 15-17):
    • Structure pipeline functions with type hints, docstrings, *args / **kwargs logging, and early return Guard Clauses.
    • Incorporate functional tools (sorted() with custom tuple lambda keys).
  5. Indefinite & Definite Loops (Lessons 13-14):
    • Run the main application inside a continuous while True interactive menu loop.
    • Iterate through record streams using for loops with enumerate() and .items().
  6. Pattern Matching CLI Dispatcher (Lesson 12):
    • Process user CLI command tokens inside a match-case block:
      • 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.
  7. 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).

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)
        

10. What We Will Learn Next

Next Up: Lesson 26 — Nested Collections, Sorting and Data Modelling

Now that you master all core data structures and comprehensions, we will explore modeling complex multi-level data architectures!

In the next lesson, we will cover:

  • Structuring complex multi-level collections (Lists of Dictionaries, Dicts of Sets).
  • Multi-level data modeling strategies for real-world enterprise applications.
  • Advanced sorting on nested collections using operator.itemgetter and attrgetter.
  • Deep searching and recursive traversal techniques through nested structures.
  • Structural Pattern Matching on complex deeply-nested data structures.

📝 Live Lesson Practice

HTML/CSS JavaScript Python C++ C PHP
⌨️ Practice Inputs (लाइव इनपुट भरें) (खाली होने पर RED, भरने पर GREEN underline)
💻 Code Editor (Monaco VS Code Engine)
👀 Live Preview
Address Contact

+91 7877547686

E-mail

onlinecbtportal@gmail.com

Helpline Number

+91 7877547686


Click To Download
Get it on Google Play

ऐप डाउनलोड करने
के लिए Google Play पर
उपलब्ध है

WhatsApp Chat