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)