PYTHON
Efficient Data Transformation with Python List and Generator Expressions
Master Python's list comprehensions for concise and efficient list creation and transformation, and leverage generator expressions for memory-efficient iteration, ideal for handling large datasets in your web applications.
# Original list of products (e.g., from a database query or API)
products = [
{"id": 1, "name": "Laptop", "price": 1200, "category": "Electronics"},
{"id": 2, "name": "Mouse", "price": 25, "category": "Electronics"},
{"id": 3, "name": "Keyboard", "price": 75, "category": "Electronics"},
{"id": 4, "name": "Desk Chair", "price": 250, "category": "Furniture"},
{"id": 5, "name": "Monitor", "price": 300, "category": "Electronics"}
]
# 1. List Comprehension: Extract product names
product_names = [p["name"] for p in products]
# print(f"Product names: {product_names}")
# Expected: ['Laptop', 'Mouse', 'Keyboard', 'Desk Chair', 'Monitor']
# 2. List Comprehension with Filtering: Get expensive electronics (price > 100)
expensive_electronics = [
p["name"] for p in products
if p["category"] == "Electronics" and p["price"] > 100
]
# print(f"Expensive electronics: {expensive_electronics}")
# Expected: ['Laptop', 'Monitor']
# 3. List Comprehension with Transformation: Apply a discount to all prices
discounted_products = [
{**p, "price": p["price"] * 0.9} # Use dict unpacking to create new dicts
for p in products
]
# print(f"Discounted products: {discounted_products}")
# Expected: List of dicts with prices reduced by 10%
# 4. Generator Expression: Sum of prices (memory efficient for large datasets)
# Note: Generator expressions create an iterator, not a list.
total_price_gen = sum(p["price"] for p in products)
# print(f"Total price (generator): {total_price_gen}")
# Expected: 1850
# 5. Nested List Comprehension: Flatten a list of lists
matrix = [[1, 2, 3], [4, 5], [6, 7, 8, 9]]
flat_list = [num for sublist in matrix for num in sublist]
# print(f"Flat list: {flat_list}")
# Expected: [1, 2, 3, 4, 5, 6, 7, 8, 9]
How it works: List comprehensions offer a concise and readable way to create new lists by applying operations to each item in an existing iterable, optionally including filters. They are highly optimized for performance. Generator expressions are similar but create an iterator instead of a full list, making them extremely memory-efficient for processing very large datasets as items are generated on-the-fly. This snippet showcases extraction, filtering, transformation, and flattening of data using these powerful Python features.