TracksPractical Coding FoundationsWorking With DataSimple Data Transformations(8 of 9)

Simple Data Transformations

Once you can store and access data in lists and dictionaries, the next step is transforming that data into something more useful. Data transformation means converting information from one form to another — and it's one of the most common tasks in programming.

Three operations form the foundation of most data transformations: filtering, mapping, and aggregating. Master these, and you'll handle the majority of data manipulation tasks you encounter.

Filtering: Keeping What You Need

Filtering creates a new collection containing only items that match a condition. You're not modifying the original — you're building something new from it.

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Keep only even numbers
evens = []
for n in numbers:
    if n % 2 == 0:
        evens.append(n)

print(evens)  # [2, 4, 6, 8, 10]

The pattern is straightforward: loop through items, check a condition, and add matching items to a new list. The original numbers list stays unchanged.

Mapping: Transforming Each Item

Mapping applies the same operation to every item, producing a new collection of transformed values. Think of it as running each item through a machine that changes it somehow.

numbers = [1, 2, 3, 4, 5]

# Double each number
doubled = []
for n in numbers:
    doubled.append(n * 2)

print(doubled)  # [2, 4, 6, 8, 10]

You could transform strings to uppercase, convert temperatures from Celsius to Fahrenheit, or extract specific fields from dictionaries. The key is that every input produces exactly one output.

Aggregating: Combining Into One Value

Aggregation reduces a collection to a single value. Summing numbers, counting items, or finding the maximum are all aggregations.

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

# Sum all numbers
total = 0
for n in numbers:
    total = total + n

print(total)  # 55

# Count items matching a condition
even_count = 0
for n in numbers:
    if n % 2 == 0:
        even_count = even_count + 1

print(even_count)  # 5

Aggregation starts with an initial value (like 0 for sums) and updates it as you process each item.

Combining Transformations

Real tasks often chain these operations. You might filter a list of users to find active ones, map them to extract just their email addresses, then count how many you have.

users = [
    {"name": "Alice", "active": True},
    {"name": "Bob", "active": False},
    {"name": "Carol", "active": True}
]

# Get names of active users
active_names = []
for user in users:
    if user["active"]:
        active_names.append(user["name"])

print(active_names)  # ["Alice", "Carol"]

These patterns appear everywhere — in data analysis, web applications, and automation scripts. Once you recognize them, you'll see opportunities to use them constantly.

See More

Further Reading

You need to be signed in to leave a comment and join the discussion