Lesson 23 of 55
9 mins readPython Zipping & Combining Lists (zip, zipWith)
In Plain English
zip combines two lists into a single list of pairs. zipWith generalizes this by applying an arbitrary binary function across corresponding element pairs.
Deep Dive: How It Works
zip: zip [1, 2, 3] ["a", "b", "c"] evaluates to [(1, "a"), (2, "b"), (3, "c")].
zipWith: zipWith (+) [1, 2, 3] [10, 20, 30] evaluates to [11, 22, 33].
Differing Lengths: Zipping stops automatically as soon as the shorter list ends.
Core Rules to Remember

zip for Pairs: Pairs elements from two collections together.

zipWith for Element-Wise Math: Vector addition, dot products, and coordinate combinations.
Live Interactive Example
Hit Run Code to see it liveVector Math with zipWith
Python 3.12
1
2
3
4
5
6
7
8
9
10
11
12
13
Output Console
Click "Run Code" to view the rendered output.
How it works: 1*4 + 2*5 + 3*6 = 4 + 10 + 18 = 32.0.
Your Turn: Micro Challenge
No pressure! Edit the starter code below and test your solution with instant feedback.
Micro Exercise
Sum Lists with zipWith
Compute `zipWith (+) [10, 20, 30] [1, 2, 3]`.
Print `"Sums: "` followed by the result.
1
2
3
4
5
6
Sandbox Output
Click "Run & Check" to test your solution.
Finished reading and practicing?
Mark this lesson as completed to update your course progress.