1. The Big Four Collections π
Python has four primary built-in data collections. Choosing the right one depends entirely on your data structure requirements (e.g., whether order matters or if duplicates are allowed).
| Collection | Ordered? | Mutable (Editable)? | Duplicates Allowed? | Syntax | Best Used For... |
|---|---|---|---|---|---|
| List | β Yes | β Yes | β Yes | [] |
Sequential data, logs, stacks of items. |
| Tuple | β Yes | β No | β Yes | () |
Fixed structures, coordinate pairs, database rows. |
| Set | β No | β Yes | β No | {} |
Eliminating duplicates, mathematical operations. |
| Dictionary | β Yes (3.7+) | β Yes | β No (Keys must be unique) | {"k": "v"} |
Fast lookups, JSON data, structured records. |
π± Easy Starter Example
my_list = [1, 2, 2, 3] # Allows duplicates: [1, 2, 2, 3]
my_tuple = (1, 2, 3) # Can't change it later!
my_set = {1, 2, 2, 3} # Drops duplicates automatically: {1, 2, 3}
my_dict = {"apple": 5} # Key-value lookup map
Real-World Application Example π οΈ
# List: Maintaining a sequence of logs
error_logs = ["404 Not Found", "500 Internal Error", "404 Not Found"]
error_logs.append("403 Forbidden")
# Tuple: A strict 3D coordinate point
point_3d = (12.5, 45.0, -3.1)
# Set: Stripping out duplicates instantly
unique_errors = set(error_logs)
print(unique_errors) # Output: {'500 Internal Error', '403 Forbidden', '404 Not Found'}
# Dictionary: Mapping a user record
user_profile = {"username": "coder_99", "role": "Admin", "verified": True}
2. Powerful Iteration Helpers βοΈ
When transforming data, looping with a basic index counter is an anti-pattern. Instead, Python offers three foundational functions to loop like a pro.
π’ enumerate()
Tracks both the index and the item value simultaneously while looping through a sequence.
- π± Easy Starter Example:
for index, letter in enumerate(["a", "b", "c"]):
print(index, letter) # Output: 0 a, 1 b, 2 c
- Real-World Application:
tasks = ["Download data", "Clean missing values", "Train model"]
for index, task in enumerate(tasks, start=1):
print(f"Step {index}: {task}")
π€ zip()
Pairs up elements from multiple lists or tuples into matched pairs. It stops as soon as the shortest list runs out.
- π± Easy Starter Example:
print(list(zip([1, 2], ["apple", "banana"])))
# Output: [(1, 'apple'), (2, 'banana')]
- Real-World Application:
names = ["Alice", "Bob", "Charlie"]
scores = [85, 92, 78]
for name, score in zip(names, scores):
print(f"{name} scored {score}%")
π sorted()
Returns a new sorted list from any iterable without modifying the original data structure.
- π± Easy Starter Example:
print(sorted([3, 1, 2])) # Output: [1, 2, 3]
- Real-World Application:
temperatures = [32, 12, 45, 22]
descending_temps = sorted(temperatures, reverse=True) # [45, 32, 22, 12]
3. Lambda Functions (Anonymous Functions) β‘
A lambda function is a small, one-line anonymous function defined without the standard def keyword. They are ideal for quick, throwaway sorting or mapping operations.
Syntax:
lambda arguments: expression
π± Easy Starter Example
# A simple function that doubles a number
double = lambda x: x * 2
print(double(5)) # Output: 10
Real-World Application Example: Sorting Complex Structures π
products = [
{"name": "Laptop", "price": 1200},
{"name": "Mouse", "price": 25},
{"name": "Monitor", "price": 300}
]
# Sort the products list by price using a lambda function as the sorting key
sorted_by_price = sorted(products, key=lambda item: item["price"])
print(sorted_by_price)
# Output: [{'name': 'Mouse', 'price': 25}, {'name': 'Monitor', 'price': 300}, ...]
4. Comprehensions (Data Transformation Engines) ποΈ
Comprehensions provide a concise, readable syntax to create a new collection based on an existing collection, effectively replacing bulky for loops.
π§Ί List Comprehension
- π± Easy Starter Example:
# Double every number in a list
doubled = [x * 2 for x in [1, 2, 3]] # Output: [2, 4, 6]
- Real-World Application:
prices_usd = [10, 25, 50]
prices_eur = [price * 0.92 for price in prices_usd] # [9.2, 23.0, 46.0]
# With a condition filter (Only keep items over $20)
expensive_items = [price for price in prices_usd if price >= 25] # [25, 50]
πΊοΈ Dictionary & Set Comprehensions
- π± Easy Starter Example:
# Create a quick number-to-square map
squares = {x: x*x for x in [1, 2, 3]} # Output: {1: 1, 2: 4, 3: 9}
- Real-World Application:
# Set Comprehension: Extract unique file extensions, converted to lowercase
raw_files = ["Data.CSV", "script.py", "readme.md", "Archive.csv"]
unique_extensions = {file.split(".")[-1].lower() for file in raw_files}
print(unique_extensions) # Output: {'csv', 'py', 'md'}
5. Practice Challenge: Processing & Transforming Structured Data ποΈ
Let's tie Day 2 together! Imagine you receive a messy API payload of users. Your task is to filter out inactive users, format their names, and map their IDs to their profile details.
# Raw structured input data
raw_users = [
{"id": 101, "name": "alice smith", "is_active": True},
{"id": 102, "name": "BOB JOHNSON", "is_active": False},
{"id": 103, "name": "charlie brown", "is_active": True},
]
# 1. Use list comprehension to filter out inactive users and format names cleanly
active_users = [
{"id": user["id"], "name": user["name"].title()}
for user in raw_users if user["is_active"]
]
print("Active Users:", active_users)
# Output: [{'id': 101, 'name': 'Alice Smith'}, {'id': 103, 'name': 'Charlie Brown'}]
# 2. Use dictionary comprehension to build a fast ID lookup database
user_database = {user["id"]: user["name"] for user in active_users}
print("User Database Map:", user_database)
# Output: {101: 'Alice Smith', 103: 'Charlie Brown'}
Top comments (0)