Skip to main content
MahiDev — Learn. Build. Become AI-ready.

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().

Intermediate40 min
map transforms every salary

What you will learn

  • Syntax varname = map(FunctionName, iterable)
  • 50% hike as sal + sal * (50 / 100) and as sal * 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.0 so 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.

map multiplies salaries 100 200 300 by 1.5 to 150 300 450

Syntax: map(FunctionName, iterable)

How map() executes

Each value is sent into the callback function, transformed, and returned to a new collection.

map reads one value, runs the callback, then creates a new value

The number of items stays exactly the same. map sends every element through FunctionName. Length stays the same; values change.

text
varname = map(FunctionName, iterable)

50% salary hike

The lesson shows two equivalent ways to increase salary by 50%.

two hike formulas sal plus half and sal times 1.50 both yield 150

Both formulas produce the same transformed result.

python
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:

python
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.

numbers 2 3 4 mapped to squares 4 9 16 and roots 1.41 1.73 2.00

Two different map() calls create two parallel lists:

  • Square → x ** 2
  • Root → x ** 0.5
python
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.

zip pairs columns A B C so each row belongs 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.

map adds 10 plus 1, 20 plus 2, 30 plus 3 to 11 22 33

Callback: lambda x, y: x + y

Position 1 adds with Position 1, Position 2 with Position 2, and so on.

python
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.

shorter list padded with 0.0 so 10 20 30 plus 1 2 3 4 keep every row

Why add 0.0? It preserves every row during addition instead of truncating the longer list.

MapEx5 equalizes first:

python
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.

original list to map transforms to new lists then zip pairs rows

Common mistakes

  • Printing the map object without list()
  • Using a one-parameter lambda with two iterables — TypeError
  • Assuming zip / map pad 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

  1. Change hike to 25% (sal + sal * (25 / 100)).
  2. Remove the padding block and watch the last 4.0 vanish from the sum table.
  3. Zip only lst and sqrlist — two columns.

Next: reduce().

Loading playground…

Check your understanding

map() and zip() quiz

What is `type(map(hike, oldsal))`?

1 / 10