Project: Data Filtering with Comprehensions
Project: Data Filtering with Comprehensions
Welcome to the Chapter 1 Capstone Project! In the preceding lessons, you mastered advanced list comprehensions, set comprehensions, dictionary comprehensions, and nested multi-dimensional structures.
Now, you will combine all these comprehension paradigms to build a production-grade Domestic Flight Booking Intelligence Engine. You will filter raw aviation manifests, deduplicate routes, aggregate airline pricing, and compute route matrices in declarative, high-speed Python.
Real-World Analogy: MakeMyTrip Flight Search Engine
When a traveler searches for flights between Delhi and Bengaluru on a booking aggregator like MakeMyTrip or Ixigo:
+-------------------------------------------------------------------------+ | FLIGHT SEARCH & INTELLIGENCE ENGINE PIPELINE | +-------------------------------------------------------------------------+ | | | Raw Flight Feed (10,000+ flights from DGCA / Airport Radar) | | │ | | ▼ | | 1. Set Comprehension ──> Extract Unique Airlines & Destination | | Hubs for Dropdown Filter Menus | | │ | | ▼ | | 2. List Comprehension ──> Filter flights by User Budget (₹), | | with Walrus (:=) Non-Stop status & Departure Window | | │ | | ▼ | | 3. Dict Comprehension ──> Group Filtered Flights by Airline & | | Compute Lowest Fare & Average Price | | │ | | ▼ | | 4. Nested Comprehension ──> Generate City-to-City Route Availability| | Matrix for Quick Comparison | | | +-------------------------------------------------------------------------+
Every stage of this data pipeline can be executed concisely and expressively using Python's comprehension toolkit.
Project Specification & Raw Dataset
The input data represents flight records received from domestic air traffic coordination:
Complete Production-Grade Implementation
Here is the modular, fully runnable Flight Intelligence Engine:
Expected Output
Best Practices & Comparison: Do's and Don'ts
| Practice | Bad Implementation | Gold-Standard Implementation |
|---|---|---|
| Deduplication | Looping over items and checking if city not in list | Set comprehension: {f['origin'] for f in manifest} |
| Price Filtering | Manual accumulator list with multiple nested ifs | Single multi-condition list comprehension |
| Aggregations | Iterating 10 times to find minimums | Dict comprehension with min() and sum() generators |
| Matrix Setup | Manual nested loops appending row by row | [[expr for col in cols] for row in rows] |
| Readability | Giant single-line expressions without formatting | Format comprehensions with clear vertical indentation |
Quick Revision Summary Cheat Sheet
- Set Comprehensions: Deduplicate unique identifiers (airports, airlines, user IDs) in $O(n)$ time.
- List Comprehensions: Filter and transform search results with multi-predicate conditions (
if f['origin'] == origin if f['price'] <= budget). - Dictionary Comprehensions: Compute group-level summaries and analytical indexes (
{k: min(...) for k in unique_keys}). - Nested Comprehensions: Generate cross-tabulation and adjacency matrices (
[[count for dst in hubs] for orig in hubs]).
Multiple Choice Questions
1. In the flight search engine, why is a set comprehension chosen to extract airport codes from the raw manifest?
A. Because airport codes must be converted into floats B. Because it automatically discards repeated airport codes, returning each unique airport hub exactly once C. Because set comprehensions sort items numerically D. Because set comprehensions require administrative permissions Answer: B Explanation: A set comprehension enforces uniqueness using a hash table. Out of thousands of flight records, each distinct airport code is retained once without duplicates.
2. How does the operator | function between two sets: origins | destinations?
A. Performs bitwise XOR on the string characters B. Computes the mathematical set Union, combining all unique elements from both sets C. Deletes matching elements D. Raises a TypeError Answer: B Explanation: The pipe operator | on Python sets performs a mathematical Set Union, merging unique elements from both sets into a single combined set.
3. What does the expression min(f["price"] for f in route_flights if f["airline"] == airline) inside the dictionary comprehension compute?
A. The total price of all flights B. The lowest ticket price offered by that specific airline on the route C. The flight with the shortest duration D. The average flight cost Answer: B Explanation: It passes a generator expression into Python's built-in min() function, filtering for flights operated by the given airline and extracting the minimum price.
4. In the route matrix comprehension [[len(...) for dst in hubs] for orig in hubs], what does the outer loop represent?
A. The destination airport column B. The origin airport row C. The total flight price D. The airline carrier name Answer: B Explanation: In a nested list comprehension [[... for inner] for outer], the outer loop corresponds to the outer dimension (rows) and the inner loop corresponds to columns. Here, orig defines each origin airport row.
5. Why is {f["flight_no"]: f for f in flights} a valid dictionary comprehension?
A. Because flight numbers are unique identifiers that serve as hashable dictionary keys B. Because flight numbers are floating-point numbers C. Because dictionary values cannot be dictionaries D. It is not valid Python syntax Answer: A Explanation: Flight numbers are immutable strings that uniquely identify individual flights, making them ideal hash keys for fast $O(1)$ dictionary lookups of complete flight record objects.
Practice Challenge
Scenario: Red-Eye Night Flight Discount Filter
Airlines offer promotional discounts on late-night Red-Eye flights (flights departing between 21:00 (9 PM) and 05:00 (5 AM)):
- 1Write a list comprehension that filters only Red-Eye flights from
FLIGHT_MANIFEST(f["dep_hour"] >= 21 or f["dep_hour"] <= 5). - 2Apply a 20% promotional discount to the base price:
price * 0.80. - 3Format each output record as:
"[DISCOUNTED] <FlightNo> (<Airline>) - New Price: ₹<Price> (Dep: <Hour>:00 hrs)".
Starter Code
Complete Solution
Expected Output
Arguments Recap (*args, **kwargs)
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Nested Comprehensions | Arguments Recap (*args, **kwargs) |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.