π Day 12: List Comprehension - Elegant Data Manipulation
In data analysis, you constantly create new lists by transforming or filtering existing ones. While for loops work, Python provides a more concise, powerful, and often faster way to do this: a list comprehension.
The Syntax of List Comprehension
A list comprehension creates a new list in a single, readable line.
new_list = [expression for item in iterable if condition]
[expression ... ]: The brackets that create the new list.expression: What to do with each item (e.g.,item * 1.1,item["name"]).for item in iterable: The loop part that iterates through the original list.if condition(Optional): A filter that decides whether to include the item.
# The for loop way
large_sales = []
for sale in sales:
if sale > 1000:
large_sales.append(sale)
# The list comprehension way
large_sales = [sale for sale in sales if sale > 1000]
Environment Setup
Before you begin, ensure you have followed the setup instructions in the main README.md to set up your virtual environment and install the required libraries.
Exploring the Refactored Code
The script for this lesson, list_comprehension.py, has been refactored to place each list comprehension task into its own testable function.
- Review the Code: Open
Day_12_List_Comprehension/list_comprehension.py. Notice the functionsapply_price_increase(),filter_large_sales(), andget_top_sales_performers(). - Run the Script: From the root directory of the project (
Coding-For-MBA), run the script to see the functions in action:python Day_12_List_Comprehension/list_comprehension.py - Run the Tests: You can run the tests for this lesson to verify the correctness of each function:
pytest tests/test_day_12.py
π» Exercises: Day 12
-
Calculate Sales Commissions:
-
In a new script (
my_solutions_12.py), create a list of sales figures:sales = [2500, 8000, 12000, 5500]. - Write a function
calculate_commissions(sales_list, commission_rate)that takes a list of sales and a rate. - Inside the function, use a list comprehension to return a new list of calculated commissions.
-
Call your function with a 15% commission rate (
0.15) and print the result. -
Filter Products by Category:
-
You have a list of product dictionaries (see below).
- Create a function
filter_by_category(product_list, category)that takes a list of products and a category name. - The function should use a list comprehension to return a new list containing only the names of products in the specified category.
-
Call your function with the category
"electronics"and print the result.products = [ {"name": "Laptop", "category": "electronics"}, {"name": "T-Shirt", "category": "apparel"}, {"name": "Keyboard", "category": "electronics"} ] -
Format Prices for Display:
-
You have a list of prices as floats:
prices = [49.99, 199.99, 19.95]. - Use a list comprehension to create a new list called
display_prices. - Each item in the new list should be a string, formatted as currency (e.g.,
"$49.99"). - Print the
display_priceslist.
π Excellent! List comprehensions are a powerful tool for writing clean, efficient, and professional Python code. They are heavily used in data analysis for quick and readable data transformations.
Previous: Day 11 β Day 11: Functions - Creating Reusable Business Tools β’ Next: Day 13 β Day 13: Higher-Order Functions & Lambda
You are on lesson 12 of 108.
Additional Materials
- list_comprehension.ipynb π View on GitHub π Run in Google Colab βοΈ Run in Binder
- solutions.ipynb π View on GitHub π Run in Google Colab βοΈ Run in Binder
list_comprehension.py
"""
Day 12: Elegant Data Manipulation with List Comprehensions (Refactored)
This script demonstrates how to use list comprehensions to
efficiently transform and filter lists of business data. This version
is refactored into functions for better organization and testability.
"""
def apply_price_increase(prices, increase_percentage):
"""
Applies a percentage price increase to a list of prices
using a list comprehension.
List comprehensions are a concise way to create new lists by
transforming each item in an existing list. They're faster and
more "Pythonic" than traditional for loops.
Syntax: [expression for item in iterable]
Parameters
----------
prices : list of float
Original prices
increase_percentage : float
Percentage increase as decimal (e.g., 0.10 for 10%)
Returns
-------
list of float
New prices with increase applied
Example
-------
>>> apply_price_increase([100, 200], 0.10)
[110.0, 220.0]
"""
increase_multiplier = 1 + increase_percentage
# List comprehension: [what_to_do_with_each_item for item in list]
# This creates a new list by multiplying each price by the multiplier
# Equivalent to a for loop but more concise:
# new_prices = []
# for price in prices:
# new_prices.append(price * increase_multiplier)
return [price * increase_multiplier for price in prices]
def filter_large_sales(sales, threshold):
"""
Filters a list of sales to find those above a given threshold
using a list comprehension with a conditional.
List comprehensions can also filter data by adding an 'if' condition.
Syntax: [expression for item in iterable if condition]
Parameters
----------
sales : list of float
List of sale amounts
threshold : float
Minimum sale amount to include
Returns
-------
list of float
Sales that exceed the threshold
Example
-------
>>> filter_large_sales([500, 1200, 800, 1500], 1000)
[1200, 1500]
"""
# List comprehension with filtering: [item for item in list if condition]
# Only includes sales where the amount is greater than threshold
# Equivalent traditional loop:
# result = []
# for sale in sales:
# if sale > threshold:
# result.append(sale)
return [sale for sale in sales if sale > threshold]
def get_top_sales_performers(employees, sales_target):
"""
Filters and transforms a list of employee dictionaries to get the
names of top-performing sales staff.
This demonstrates a more complex list comprehension that:
1. Filters by multiple conditions (AND logic)
2. Extracts just one field (name) from dictionaries
Parameters
----------
employees : list of dict
List of employee dictionaries with 'name', 'department', 'quarterly_sales'
sales_target : float
Minimum sales to be considered top performer
Returns
-------
list of str
Names of sales employees who exceeded the target
Example
-------
>>> employees = [
... {"name": "Alice", "department": "Sales", "quarterly_sales": 12000},
... {"name": "Bob", "department": "Engineering", "quarterly_sales": 0},
... ]
>>> get_top_sales_performers(employees, 10000)
['Alice']
"""
# Complex list comprehension with multiple conditions:
# [what_to_extract for item in list if condition1 and condition2]
#
# This extracts employee['name'] for each employee where:
# 1. Their department is "Sales" AND
# 2. Their quarterly_sales exceeds sales_target
#
# .get() with a default of 0 prevents errors if key is missing
return [
employee["name"]
for employee in employees
if employee.get("department") == "Sales"
and employee.get("quarterly_sales", 0) > sales_target
]
def main():
"""Main function to demonstrate list comprehensions."""
# --- Example 1: Transforming Data ---
print("--- Applying a Price Increase ---")
original_prices = [100.00, 150.50, 200.00, 80.25]
print(f"Original prices: {original_prices}")
increased_prices = apply_price_increase(original_prices, 0.10) # 10% increase
print(f"New prices (from comprehension): {[f'${p:.2f}' for p in increased_prices]}")
print("-" * 20)
# --- Example 2: Filtering Data ---
print("--- Filtering for Large Sales Transactions ---")
sales_data = [500, 1200, 800, 1500, 300, 2500]
print(f"All sales: {sales_data}")
large_sales_data = filter_large_sales(sales_data, 1000)
print(f"Large sales (from comprehension): {large_sales_data}")
print("-" * 20)
# --- Example 3: Filtering and Transforming ---
print("--- Extracting Names of High-Performing Sales Staff ---")
employee_data = [
{"name": "Alice", "department": "Sales", "quarterly_sales": 12000},
{"name": "Bob", "department": "Engineering", "quarterly_sales": 0},
{"name": "Charlie", "department": "Sales", "quarterly_sales": 8000},
{"name": "David", "department": "Sales", "quarterly_sales": 15000},
]
target = 10000
top_performers_list = get_top_sales_performers(employee_data, target)
print(f"Top performing sales staff (sales > ${target}): {top_performers_list}")
print("-" * 20)
if __name__ == "__main__":
main()
solutions.py
"""
Day 12: Solutions to Exercises
"""
# --- Exercise 1: Calculate Sales Commissions ---
print("--- Solution to Exercise 1 ---")
sales = [2500, 8000, 12000, 5500]
commission_rate = 0.10
# [expression for item in iterable]
# The expression is the calculation for each sale.
commissions = [sale * commission_rate for sale in sales]
print(f"Original sales: {sales}")
print(f"Calculated commissions (10%): {commissions}")
print("-" * 20)
# --- Exercise 2: Filter Products by Category ---
print("--- Solution to Exercise 2 ---")
products = [
{"name": "Laptop", "category": "electronics"},
{"name": "T-Shirt", "category": "apparel"},
{"name": "Keyboard", "category": "electronics"},
{"name": "Coffee Mug", "category": "homeware"},
{"name": "Webcam", "category": "electronics"},
]
# [expression for item in iterable if condition]
# The expression is the product's name.
# The condition checks if the product's category is 'electronics'.
electronic_products = [
product["name"] for product in products if product["category"] == "electronics"
]
print(f"All products: {products}")
print(f"Electronic products only: {electronic_products}")
print("-" * 20)
# --- Exercise 3: Format Prices for Display ---
print("--- Solution to Exercise 3 ---")
prices = [49.99, 199.99, 19.95, 24.50, 12.00]
# The expression is an f-string that formats each price.
display_prices = [f"${price:,.2f}" for price in prices]
print(f"Original prices (float): {prices}")
print(f"Display prices (string): {display_prices}")
print("-" * 20)