Python Zip Function With Examples | Python Unzipping values

Free Python courses 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

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

  • 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.
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.

Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google

follow dataflair on YouTube

4 Responses

  1. Uzma sardar says:

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

    • 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. 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 🙂

    • 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 *