DEV Community

Thiruvengadam Sakthivel
Thiruvengadam Sakthivel

Posted on

Day 2 – Collections & Data Transformation

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

Enter fullscreen mode Exit fullscreen mode

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}

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode
  • Real-World Application:
tasks = ["Download data", "Clean missing values", "Train model"]
for index, task in enumerate(tasks, start=1):
    print(f"Step {index}: {task}")

Enter fullscreen mode Exit fullscreen mode

🀐 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')]

Enter fullscreen mode Exit fullscreen mode
  • Real-World Application:
names = ["Alice", "Bob", "Charlie"]
scores = [85, 92, 78]
for name, score in zip(names, scores):
    print(f"{name} scored {score}%")

Enter fullscreen mode Exit fullscreen mode

πŸ“ 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]

Enter fullscreen mode Exit fullscreen mode
  • Real-World Application:
temperatures = [32, 12, 45, 22]
descending_temps = sorted(temperatures, reverse=True)  # [45, 32, 22, 12]

Enter fullscreen mode Exit fullscreen mode

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

Enter fullscreen mode Exit fullscreen mode

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}, ...]

Enter fullscreen mode Exit fullscreen mode

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]

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

Enter fullscreen mode Exit fullscreen mode

πŸ—ΊοΈ 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}

Enter fullscreen mode Exit fullscreen mode
  • 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'}

Enter fullscreen mode Exit fullscreen mode

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'}

Enter fullscreen mode Exit fullscreen mode

Top comments (0)