← QCE Digital Solutions Unit 2

Transforming a stream of records

map() applies a function to every record in a stream and gives back changed records, such as converting a type, trimming and changing the case of text, or computing a derived field.

Understanding Transforming a stream of records in QCE Digital Solutions Unit 2

What it does: The comprehension applies float() to the price field of each record, giving back changed values. The new total field is computed from the price and qty fields, so it is a derived field. Values read from a file arrive as text. Converting the type, such as float("12.50"), is a transform that lets you do maths on the value. strip() removes spaces from both ends and title() capitalises each word, giving "Ava Nguyen". This is a trim and change-case transform. map() changes each record but never drops any. Removing records is the job of filtering. Changing everything to lower case makes the values consistent so they match when compared. strip("red") would remove letters instead.

Common mistakes: map() needs the function itself, not a call to it. int() with no argument raises an error. It should be map(int, rows), which converts each string to an integer. The list is missing its closing square bracket. The line should end with ] so the list of raw names is complete before each one is trimmed and title-cased.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 186–228) and word search (Modules 144–228).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
A stream of records holds prices as text, such as "12.50". Which step converts each price so it can be used in calculations?
Leave the price as text and add 1 to itConvert each price with float(row[2])
Matches
price = row[2] total = price * 2
↓
row[2] is still a string from the CSV file, so it must be converted with int() or float() first. Otherwise the multiplication repeats the text instead of calculating a number.
Applying
Meanings
Term
map
Meaning
map() applies a function to every record in a stream and gives back changed records, such as converting a type, trimming and changing the case of text, or computing a derived field.
Practice
lines = [" ava,88", "LIAM,65 ", " mia,95"]
Testing
Gaps
prices = list((float, rawprices))
Check Answer
Spots
1rows = ["5", "12", "8"]
2nums = list(map(int(), rows))

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Owl' badge — Wise Coder, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. A stream of records holds prices as text, such as "12.50". Which step converts each price so it can be used in calculations?
A. Convert each price with float(row[2]) ✓
B. Leave the price as text and add 1 to it
Values read from a file arrive as text. Converting the type, such as float("12.50"), is a transform that lets you do maths on the value.
2. Which line transforms every record in the stream with map()?
A. clean = map(tidy, records) ✓
B. clean = filter(tidy, records)
map() applies a function to every record and gives back a changed version of each one. filter() only keeps or drops records and does not change them.
3. A student name is stored as " aVA nguyen ". Which expression trims the spaces and fixes the case?
A. name.split().count()
B. name.strip().title() ✓
strip() removes spaces from both ends and title() capitalises each word, giving "Ava Nguyen". This is a trim and change-case transform.
4. After transforming a stream with map(), how many records come out compared with how many went in?
A. Fewer, because map() removes some records
B. The same number, each one changed ✓
map() changes each record but never drops any. Removing records is the job of filtering.
5. Each record has price and qty. Which is a derived field transform?
A. total = price * qty ✓
B. total = price + "qty"
A derived field is a new value worked out from existing fields. Multiplying price by qty gives a total for each record.

Match the Pairs

int(row[2]) → Converts a text field from a CSV line into a whole number so it can be used in calculations
name.strip().title() → Removes extra spaces from both ends of the text and capitalises each word
Transform every record in a stream by converting its price field from text to a float → [float(r[1]) for r in rows]
Add a derived field to each record by calculating a new value from existing fields → total = price * qty
price = row[2] total = price * 2 → row[2] is still a string from the CSV file, so it must be converted with int() or float() first. Otherwise the multiplication repeats the text instead of calculating a number.
name.upper → The brackets are missing, so the method is not called and the text is never changed. It should be name.upper().

Key Term

map
map() applies a function to every record in a stream and gives back changed records, such as converting a type, trimming and changing the case of text, or computing a derived field.

Fill in the Blanks

1. prices = list(map(float, raw_prices))
Hint: Type the built-in function that applies a function to every record in the stream.
2. ages = list(map(int, ages_text))
Hint: Type the function that converts each text value into a whole number.
3. names = list(map(lambda s: s.strip(), raw))
Hint: Type the string method that trims spaces from both ends of each text value.
4. gross = list(map(lambda p: p * 1.1, nums))
Hint: Type the arithmetic operator that computes the derived value by multiplying each record.
5. shout = list(map(str.upper, houses))
Hint: Type the string method that changes each text value to capital letters.

Spot the Error

Question 1
A rows = ["5", "12", "8"]
B nums = list(map(int(), rows))
C print(nums)
Line B — map() needs the function itself, not a call to it. int() with no argument raises an error. It should be map(int, rows), which converts each string to an integer.
Question 2
A names = [" ava ", "LIAM", "mia "
B clean = []
C for n in names:
D clean.append(n.strip().title())
Line A — The list is missing its closing square bracket. The line should end with ] so the list of raw names is complete before each one is trimmed and title-cased.
Question 3
A prices = ["4.50", "12", "3.25"]
B nums = [float(p) for p in price]
C print(nums)
Line B — The variable is called prices, not price, so the comprehension fails with a NameError. It should loop over prices to convert each string to a float.
Question 4
A rows = [("Pen", 2, 1.5)]
B out = []
C for name, qty, price in rows:
D out.append((name, qty + price))
E print(out)
Line D — The derived field is a line total, which is quantity times price. Adding them gives the wrong value. It should be qty * price.
Question 5
A def clean(text):
B text = text.strip()
C return text.upper
D print(clean(" ava "))
Line C — upper is a method and must be called with brackets. Without them the function returns the method object instead of the changed text. It should be text.upper().

Student with a class code? Join your class →