Youโve learned about sets and dictionaries โ but where do they shine in real-world Python projects, especially in APIs and data pipelines? Letโs look at practical, production-friendly use cases.
๐ 1. Fast Duplicate Removal with Sets
โ
Example: Cleaning up email list before sending campaign
emails = ['alice@example.com', 'bob@example.com', 'alice@example.com']
unique_emails = list(set(emails))
๐ 2. Efficient Membership Checks
โ
Example: Check if a user has access to a resource
authorized_user_ids = {1001, 1002, 1003}
if user_id in authorized_user_ids:
return "Access granted"
โ Sets make this check O(1) โ way faster than a list.
๐ 3. Grouping API Records with Dictionaries
โ
Example: Group transactions by user ID
from collections import defaultdict
transactions = [
{'user_id': 1, 'amount': 100},
{'user_id': 2, 'amount': 50},
{'user_id': 1, 'amount': 70}
]
grouped = defaultdict(list)
for txn in transactions:
grouped[txn['user_id']].append(txn)
# grouped = {1: [...], 2: [...]}
๐ 4. Remapping or Normalizing API Payloads
โ
Example: Renaming keys from snake_case to camelCase
def to_camel_case(snake_str):
parts = snake_str.split('_')
return parts[0] + ''.join(x.title() for x in parts[1:])
payload = {'user_id': 123, 'user_name': 'alice'}
normalized = {to_camel_case(k): v for k, v in payload.items()}
๐ 5. Mapping IDs to Names for API Response Enrichment
user_map = {1: 'Alice', 2: 'Bob'}
transactions = [{'user_id': 1, 'amount': 50}, {'user_id': 2, 'amount': 100}]
for txn in transactions:
txn['user_name'] = user_map.get(txn['user_id'], 'Unknown')
โ
6. Detecting Shared Items Between Datasets
โ
Example: Users active in both services
service_a_users = {1, 2, 3}
service_b_users = {3, 4, 5}
both = service_a_users & service_b_users # {3}
โจ Summary
Problem | Tool Used |
---|---|
Remove duplicates | set() |
Fast lookup | set |
Group by field |
dict , defaultdict
|
Rename JSON keys | Dictionary comprehension |
Enrich API payloads | dict.get() |
Find common users/items | Set intersection (& ) |
Sets and dictionaries are workhorses in backend engineering, especially when building scalable APIs or ETL systems. Use them wisely to improve performance, readability, and efficiency.
Follow for more real-world backend Python tips ๐โ๏ธ
Top comments (0)