DEV Community

dev0928
dev0928

Posted on

What is a Zip function in Python?

Zip is a powerful function in Python that is often ignored by many of us as it has a completely different use and meaning outside the Python world. The concept of zipping a file or compressing a file is the most common way we have used the term zip. But Zip function has a completely different meaning and use in Python. This article attempts to explain the zip function.

What is a zip function?

Here is the definition of zip from Python's help module:

Return a zip object whose .__next__() method returns a tuple where
the i-th element comes from the i-th iterable argument. The .__next__()
method continues until the shortest iterable in the argument sequence
is exhausted and then it raises StopIteration.

Although above definition is very accurate, it does not help fully grasp the functionality of zip function. So, letโ€™s try to understand with an example.

Consider below two lists of alphabets and numbers. If we need to map the individual elements of the list, we could use a zip function:

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')
Enter fullscreen mode Exit fullscreen mode

What happens if the lists getting combined have a different number of elements?

Zip iteration stops when there are no more elements to match in one of the lists.

numbers = [1, 2, 3]
letters = ["a", "b", "c", "d", "e", "f"]
for pair in zip(numbers, letter):
    print(pair)   # (1, 'a') (2, 'b') (3, 'c') 
Enter fullscreen mode Exit fullscreen mode

Could we combine more than two iterables with zip?

Zipping could be performed with any number of iterables.

ids = [1001, 1002, 1003]
items = ["apple", "orange", "banana"]
price = [3.99, 2.99, 1.99]
for t in zip(ids, items, price):
    print(t)     #(1001, 'apple', 3.99) (1002, 'orange', 2.99) (1003, 'banana', 1.99)
Enter fullscreen mode Exit fullscreen mode

What happens if only one iterable is supplied to the zip function?

Zip function generates tuples containing one element.

word = 'hello'
for w in zip(word):
    print(w)     # ('h',) ('e',) ('l',) ('l',) ('o',)
Enter fullscreen mode Exit fullscreen mode

How can we perform the reverse of zip operation?

We could unpack the collection of tuples into individual iterables using below zip function:

src = [(1001, 'apple'), (1002, 'orange'), (1003, 'banana')]    
ids, items = tuple(zip(*src))
print(ids)      # (1001, 1002, 1003)
print(items)   # ('apple', 'orange', 'banana')
Enter fullscreen mode Exit fullscreen mode

How can I create other data structures using a zip function?

Zip function could be used to combine below iterables to make a dictionary.

ids = [1, 2, 3, 4, 5]
vowels = "aeiou"

id_vowel = dict(zip(ids, vowels))
print(id_vowel)   # {1: 'a', 2: 'e', 3: 'i', 4: 'o', 5: 'u'}
Enter fullscreen mode Exit fullscreen mode

Where can I use this zip function?

Zip function would be quite handy during cleansing and processing of multiple sources of data into something more meaningful.

Top comments (6)

Collapse
 
waylonwalker profile image
Waylon Walker

๐Ÿคฏ This example kinda blow my mind. I love *unpacking, but have never used it inside of zip.

src = [(1001, 'apple'), (1002, 'orange'), (1003, 'banana')]    
ids, items = tuple(zip(*src))
print(ids)      # (1001, 1002, 1003)
print(items)   # ('apple', 'orange', 'banana')
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 ๐Ÿคฃ