DEV Community

Discussion on: What is a Zip function in Python?

Collapse
 
waylonwalker profile image
Waylon Walker • Edited

EDIT - this is a terrible comment.. scratch that.

Important to note the problem that the zip function solves for us and that is unnecessary nesting, and creating a 〽 mountain effect in our code. Which outside of these simple examples leads to unnecessary complexity.

❌ without zip function

numbers = [1, 2, 3, 4]
letters = ["a", "b", "c", "d"]
for number in numbers
   for letter in letters:
      print(number, letter)   # (1, 'a') (2, 'b') (3, 'c') (4, 'd')

✅ Your solution is much better

numbers = [1, 2, 3, 4]
letters = ["a", "b", "c", "d"]
for pair in zip(numbers, letters):
   print(pair)   # (1, 'a') (2, 'b') (3, 'c') (4, 'd')
Collapse
 
srleyva profile image
Stephen Leyva (He/Him)

Apologies, I am a bit confused, wouldn’t the first solution iterate through letters 4 different times for each number or I misreading something? 😅😅

IE. 1,a 1,b 1,c 1,d

Collapse
 
waylonwalker profile image
Waylon Walker • Edited

😳 OMG, now that's embarrassing. I need to think 2x about untested late-night comments.

Thread Thread
 
srleyva profile image
Stephen Leyva (He/Him)

Sorry for calling it out, that was kind of dickish 😣 I looked at it for like 10 mins trying to figure out if I was crazy! 😂😂

Thread Thread
 
waylonwalker profile image
Waylon Walker

No worries on the call out! I feel really silly for posting 🤣