Site icon DataFlair

Python Zip Function With Examples | Python Unzipping values

Python course with 57 real-time projects - Learn Python

In this Python tutorial, we will discuss Python Zip Function. Moreover, we will discuss the built-in zip() function in Python with the example. Also, we will understand Zip in Python and Python Unzipping Values.

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

Python Zip Function With Examples | Python Unzipping values

What is Python Zip Function?

Like we’ve said manifold 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 Built-in Functions. Let’s take a quick recap before we can proceed to explain this to you from scratch.

zip() is a built-in Python function that gives us an iterator of tuples. Let’s take a quick example of Python Zip Function

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

Output

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

Understanding Python zip()

Like Ziploc in the real world and .zip files in the virtual one, a zip is a kind of a 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 this two lists of equal lengths.

>>> 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.
So, this was all in Python Zip Function. Hope you like our explanation.

Python Interview Questions on Zip Function

  1. What is Python zip Function? Explain with 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 Python Zip tutorial, we discussed Python Zip Functions in detail. Moreover, we saw Zip in Python with Python Zip function example and unzipping values in Python.

Furthermore, while learning Python Zip Function, if you feel any query, ask in comments.

Exit mobile version