Python Zip Function With Examples | Python Unzipping values

Stay updated with the latest technology trends while you're on the move - Join DataFlair's Telegram Channel

1. Objective – Python Zip Function

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

2. 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)

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

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

Have a look at Python NumPy Tutorial.

a. 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).

b. 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)

(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.
Let’s take a tour to Python syntax and semantics

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

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

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

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

{(1, 3), (2, 4), (None, 5)}
Do you know about Python Directory and Files

Python Interview Questions

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

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

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

5. Conclusion – Python Zip Function

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.
See also – 
Python Function Argument
For reference

2 Responses

  1. Uzma sardar says:

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

  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 🙂

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.