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 Python Zip Function with example. Also, we will understand Zip in Python and Python Unzipping Values.

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

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() 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 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

  • No arguments

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

>>> set(zip())

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

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

Output

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

This returns tuples holding single values

  • Multiple arguments of the same lengths

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’, ‘$’)
  • Multiple arguments of different lengths

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 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 tutorial, we discussed Python Zip Functions in detail with examples. Moreover, we saw unzipping values in Python.

If you feel any query, ask in comments.

Did you know we work 24x7 to provide you best tutorials
Please encourage us - write a review on Google

courses
Image

DataFlair Team

DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.

4 Responses

  1. Image Uzma sardar says:

    Very nicely explained can you please add its practical use case so as to understand in much clearer way

    • Image DataFlair Team says:

      Thanks for liking the Python Zip Function Tutorial. For more practicals along with Python Projects, we recommend you to take the Free Python Course by DataFlair.

  2. Image Ravi says:

    Your explanation is very concrete and clear. Please add some practical examples where zip function can be used. It will add to the understanding of the learner. Thanks 🙂

    • Image BABA says:

      Take for example you’re maintaining a school database and you’ve got students rolls, names, class, section in different lists.
      Now to print information of the students you may use zip.

      Obviously using dictionaries in the first place would be better but this is just a real-life(sort of) example

Leave a Reply

Your email address will not be published. Required fields are marked *