Lesson 04
Python map() and zip() — Transform Lists and Pair Results
Use map() to hike salaries and square values, map two lists with a two-parameter lambda, pad unequal lengths with 0.0, and print columns with zip().
What you will learn
- Syntax
varname = map(FunctionName, iterable) - 50% hike as
sal + sal * (50 / 100)and assal * 1.50 - Squares and square roots, then a printed table with
zip map(lambda x, y: x + y, lst1, lst2)— one lambda, two iterables- Padding the shorter list with
0.0so zip/map do not drop leftover values
Functions showed a single-list hike. This lesson is the MAP FUNCTIONS folder plus Zip.txt.
What is map()?
map() applies the same operation to every element in an iterable. Unlike filter(), it never removes items — the output has the same number of elements.
Syntax: map(FunctionName, iterable)
How map() executes
Each value is sent into the callback function, transformed, and returned to a new collection.
The number of items stays exactly the same. map sends every element through FunctionName. Length stays the same; values change.
varname = map(FunctionName, iterable)50% salary hike
The lesson shows two equivalent ways to increase salary by 50%.
Both formulas produce the same transformed result.
def hike(sal):
return sal + sal * (50 / 100)
oldsal = [100, 600, 200, 300, 400, 500]
mapobj = map(hike, oldsal)
print(type(mapobj)) # <class 'map'>
newsal = list(mapobj)Inline lambda from MapFunsEx2:
newsal = list(map(lambda sal: sal * 1.50, oldsal))100 * 1.50 is 150.0. Same idea as adding 50 percent.
Classroom demos may use input(). Here oldsal is a literal so the playground can run.
Squares and square roots
map() can generate completely different datasets from the same input — for example, squares and square roots.
Two different map() calls create two parallel lists:
- Square →
x ** 2 - Root →
x ** 0.5
lst = [25.0, 3.0, 4.0, 15.0, 226.0]
sqrlist = list(map(lambda x: x ** 2, lst))
sqroots = list(map(lambda x: round(x ** 0.5, 2), lst))
print("Given Number\tSquares\tSquare Roots")
for no, sqno, sqrtno in zip(lst, sqrlist, sqroots):
print(no, sqno, sqrtno)What is zip()?
zip() combines multiple iterables row by row. It does not modify data — it simply pairs matching positions together.
Purpose: display related values in columns.
zip walks in lockstep. Row 1 of the classroom list is 25.0, 625.0, 5.0.
Zip.txt in the teaching folder is this pairing step — not a fourth special function, but the way you display parallel sequences.
map() with two lists
map() can process multiple iterables. The callback receives one argument from each list.
Callback: lambda x, y: x + y
Position 1 adds with Position 1, Position 2 with Position 2, and so on.
lst1 = [10.0, 20.0, 30.0]
lst2 = [1.0, 2.0, 3.0]
lst3 = list(map(lambda x, y: x + y, lst1, lst2))
# [11.0, 22.0, 33.0]Without padding, extra items on the longer list are ignored (same rule as zip).
Unequal length lists
Both map() and zip() stop at the shortest iterable. The lesson pads the shorter list with 0.0 so no values are lost.
Why add 0.0? It preserves every row during addition instead of truncating the longer list.
MapEx5 equalizes first:
if len(lst1) > len(lst2):
lst2 = lst2 + [0.0] * (len(lst1) - len(lst2))
elif len(lst2) > len(lst1):
lst1 = lst1 + [0.0] * (len(lst2) - len(lst1))
lst3 = list(map(lambda x, y: x + y, lst1, lst2))
for no1, no2, no3 in zip(lst1, lst2, lst3):
print(no1, no2, no3)Adding 0.0 means “missing value contributes nothing to the sum” instead of disappearing.
Complete map() + zip() pipeline
This lesson combines transformation and presentation into one workflow.
Common mistakes
- Printing the map object without
list() - Using a one-parameter lambda with two iterables —
TypeError - Assuming
zip/mappad automatically - Mixing
**2(square) with**0.5(root) in the same lambda by accident
Interview cheat sheet
| Concept | Answer |
|---|---|
map() |
Transforms every element |
| Output size | Same as input |
| Two lists | lambda x, y |
zip() |
Pairs values by position |
| Unequal lists | Pad with 0.0 before mapping |
| Type | <class 'map'> until cast |
| 50% hike | sal + sal*(50/100) or sal * 1.50 |
Practice in the playground
- Change hike to 25% (
sal + sal * (25 / 100)). - Remove the padding block and watch the last
4.0vanish from the sum table. - Zip only
lstandsqrlist— two columns.
Next: reduce().
Loading playground…
Check your understanding
map() and zip() quiz
What is `type(map(hike, oldsal))`?
1 / 10