DEV Community

Mohammad Nadeem
Mohammad Nadeem

Posted on

πŸ› οΈ Real-World Use Cases of Sets and Dicts in APIs & Data Processing

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))
Enter fullscreen mode Exit fullscreen mode

πŸ” 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"
Enter fullscreen mode Exit fullscreen mode

βœ… 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: [...]}
Enter fullscreen mode Exit fullscreen mode

πŸ”„ 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()}
Enter fullscreen mode Exit fullscreen mode

πŸ”€ 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')
Enter fullscreen mode Exit fullscreen mode

βœ… 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}
Enter fullscreen mode Exit fullscreen mode

✨ 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)