Site icon DataFlair

Python Zip Function With Examples | Python Unzipping values

Python Zip Function

Python Zip Function With Examples | Python Unzipping values

Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python

In this Python tutorial, we will discuss the Python Zip Function with an example. Also, we will understand Zip in Python and Python Unzipping Values.

So, let’s start the Python Zip Function tutorial.

What is the Python Zip Function?

Like we’ve said manifold times before, the interpreter for Python has some types and functions built into it; these are the ones always available to it.

zip() is one such function, and we saw a brief on it when we talked about Built-in Functions. Let’s take a quick recap before we can proceed to explain this to you from scratch.

zip() ties two or more sequences side by side, making pairs (or tuples) of matching positions. Picture a zipper joining the teeth of two rails—that is exactly how zip bonds lists, tuples, or even strings.

Let’s take a quick example of the Zip Function in Python

>>> for i in zip([1,2,3],['a','b','c']):
        print(i)

Output

(1, ‘a’)
(2, ‘b’)
(3, ‘c’)

Use of Zip Function in Python

Understanding Python zip()

Like Ziploc in the real world and .zip files in the virtual one, a zip is a kind of container. Like a .zip file holds real files within itself, a zip holds real data within.

It takes iterable elements as input and returns an iterator on them (an iterator of tuples). It evaluates the iterables left to right.

1. The syntax for Python Zip Function

Python zip() function has the following syntax-

zip(*iterables)

As arguments, it can take iterables, we see. These can be built-in, like the list, string, dict, and user-defined (objects with the __iter__ method).

2. Python Zip Function Example

What happens when we provide no arguments to zip()?

>>> set(zip())

set()
You can see that this returns an empty iterator.

>>> for i in zip([1,2,3]):
        print(i)

Output

(1,)
(2,)
(3,)

This returns tuples holding single values

So, let’s pass these two lists of equal length.

>>> for i in zip([1,2,3],['a','b','c']):
        print(i)

Output

(1, ‘a’)
(2, ‘b’)
(3, ‘c’)

This zips elements together from each list. How about more than two?

>>> for i in zip([1,2,3],['a','b','c'],['#','*','$']):
        print(i)

Output

(1, ‘a’, ‘#’)
(2, ‘b’, ‘*’)
(3, ‘c’, ‘$’)

When we provide multiple lists of different lengths, it stops at the shortest one.

>>> set(zip([1,2],[3,4,5]))

Output

{(1, 3), (2, 4)}

If you want to keep those, you can borrow zip_longest() from itertools.

>>> from itertools import zip_longest as zl
>>> set(zl([1,2],[3,4,5]))

Output

{(1, 3), (2, 4), (None, 5)}

Unzipping Values in Python

Now we know how to zip values together. But how to unzip them? Well, we use the * character with the zip() function.

>>> z=zip([1,2,3],['a','b','c'],['#','*','$'])
>>> a,b,c=zip(*z)
>>> a,b,c

Output

((1, 2, 3), (‘a’, ‘b’, ‘c’), (‘#’, ‘*’, ‘$’))

So, this unzips the zip object z into the variables a, b, and c.

>>> z=zip([1,2],[3,4,5])
>>> a,b=zip(*z)
>>> a,b

Output

((1, 2), (3, 4))

Now, notice that this dropped the element 5 because it didn’t zip into anything anyway.

Python Interview Questions on Zip Function

1. What is the Python zip Function? Explain with an example.

2. What does zip() do in Python?

3. How to get a zip file in Python?

4. How do you zip two lists in Python?

5. Can Python read zip files?

Conclusion

Hence, in this tutorial, we discussed Python Zip Functions in detail with examples. Moreover, we saw unzipping values in Python.

If you have any queries, ask in the comments.

Exit mobile version