zip() Function In Python — Usage & Examples With Code

Sachin Pal
5 min readJun 30, 2022

--

Zip function in Python
Source: Author(GeekPython)

Have you ever heard the word “ parallel iteration “ or tried to “ loop over multiple iterables in parallel “ when you were coding in Python?

This tutorial will show a Python zip() function that helps us perform a parallel iteration over multiple iterables.

Definition

zip() function takes iterables and iterates over them parallelly, which results in producing tuples of each item from the iterables.

items = ["Computer", "Keyboard", "CPU", "Mouse"]
units = [3, 2, 4, 6]

mapping = zip(items, units)

print(tuple(mapping))

Output

(('Computer', 3), ('Keyboard', 2), ('CPU', 4), ('Mouse', 6))

In other words, it returns the iterator of tuples where the first item in each passed iterator is paired together and then the second item in each passed iterator is paired together and it goes so on until the shortest iterator is exhausted.

Another way to think of zip() is that it turns rows into columns and columns into rows. This is similar to transposing a matrix. Source

Syntax

The syntax of Python zip() function is:

zip(*iterables) or zip(iterator1, iterator2, ...)

In Python 3.10, the strict argument was added

zip(*iterables, strict=False)

We’ll see the use of strict ahead in this tutorial.

zip() Parameters -

iterables: they can be lists, tuples, dictionaries, or objects that can be iterated.

Example

languages = ["Python", "JavaScript", "C", "C++"]
founded = [1991, 1995, 1972, 1985]

mapping = zip(languages, founded)

print(list(mapping))

print(type(list(mapping)))

Output

[('Python', 1991), ('JavaScript', 1995), ('C', 1972), ('C++', 1985)]
<class 'list'>

Working of zip() Function

How zip() function creates an iterator of the tuple?

We can actually say that zipping means aggregating two separate things into one.

Just like it, the Python zip() function works by taking two inputs say A1 and A2, and then aggregating the item of the same index number of A1 and A2.

You will better understand through the illustration below

  • You can clearly see, on the right side — the tuple at the 0th index contains each item of A1 and A2 at the 0th index respectively.
  • The same goes for items at the 1st index of A1 and A2.
  • In general, the tuple at the index i contains items at the index i in A1 and A2.

More formally: zip() returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the argument iterables. Source

Iterables of different lengths

What happens if iterables are of different lengths?

Nothing’s gonna happen if the iterables passed to the zip() function have different lengths because if we refer to official Python docs then it states that “ the iterator stops when the shortest iterable is exhausted “.

Example

languages = ["Python", "JavaScript", "C", "C++"]
founded = [1991, 1995, 1972]

mapping = zip(languages, founded)

print(list(mapping))

Output

[('Python', 1991), ('JavaScript', 1995), ('C', 1972)]

Here, the C++ was excluded because the founded variable contains only three arguments.

What if Python throws an error when the iterables are of different lengths?

Let’s understand it by the example

languages = ["Python", "JavaScript", "C", "C++"]
founded = [1991, 1995, 1972]

mapping = zip(languages, founded, strict=True)

print(list(mapping))

Output

Traceback (most recent call last):
File "D:\SACHIN\Pycharm\PythonTasks\zip_python\main.py", line 13, in <module>
print(list(mapping))
ValueError: zip() argument 2 is shorter than argument 1

Did you notice that there is an additional argument strict=True in the code?

strict argument is added in the Python version 3.10

If the iterables are of different lengths and in that case, we use strict argument then the code will throw a ValueError.

It can be useful for debugging.

Passing one or no iterable to the zip()

The zip() function will return an empty iterator if no parameters were passed.

no_iterable = zip()
print(list(no_iterable))

Output

[]

If we pass only one iterable, then the zip() function will return an iterator of tuples each having only one element.

languages = ["Python", "JavaScript", "C", "C++"]

mapping = zip(languages)

print(list(mapping))

Output

[('Python',), ('JavaScript',), ('C',), ('C++',)]

Python zip() Examples

Example: Using enumerate() function with zip()

characters = ["Iron Man", "Thor", "Spiderman"]
real_names = ["RDJ", "Chris Hemsworth", "Andrew Garfield"]

result = zip(characters, real_names)

for num, results in enumerate(result):
print(num, results)

Output

0 ('Iron Man', 'RDJ')
1 ('Thor', 'Chris Hemsworth')
2 ('Spiderman', 'Andrew Garfield')

Example: Using range() function

name = ["Sachin", "Rishu", "Yashwant", "Abhishek"]

values = zip(range(4), name)

print(tuple(values))

Output

((0, 'Sachin'), (1, 'Rishu'), (2, 'Yashwant'), (3, 'Abhishek'))

Example: Having multiple iterables

characters = ["Iron Man", "Thor", "Spiderman"]
real_names = ["RDJ", "Chris Hemsworth", "Andrew Garfield"]
reel_names = ["Tony Stark", "Thor", "Peter Parker"]

result = zip(characters, real_names, reel_names)

print(list(result))

Output

[('Iron Man', 'RDJ', 'Tony Stark'), ('Thor', 'Chris Hemsworth', 'Thor'), ('Spiderman', 'Andrew Garfield', 'Peter Parker')]

Example: Typecasting into different data types

Typecasting into List

characters = ["Iron Man", "Thor", "Spiderman"]
real_names = ["RDJ", "Chris Hemsworth", "Andrew Garfield"]

result = zip(characters, real_names)

print(f"List: {list(result)} and type is {type(list(result))}")

Output

List: [('Iron Man', 'RDJ'), ('Thor', 'Chris Hemsworth'), ('Spiderman', 'Andrew Garfield')] and type is <class 'list'>

Typecasting into Dictionary

characters = ["Iron Man", "Thor", "Spiderman"]
real_names = ["RDJ", "Chris Hemsworth", "Andrew Garfield"]

result = zip(characters, real_names)

print(f"Dictionary: {dict(result)} and type is {type(dict(result))}")

Output

Dictionary: {'Iron Man': 'RDJ', 'Thor': 'Chris Hemsworth', 'Spiderman': 'Andrew Garfield'} and type is <class 'dict'>

Typecasting into Set

characters = ["Iron Man", "Thor", "Spiderman"]
real_names = ["RDJ", "Chris Hemsworth", "Andrew Garfield"]

result = zip(characters, real_names)

print(f"Set: {set(result)} and type is {type(set(result))}")

Output

Set: {('Iron Man', 'RDJ'), ('Spiderman', 'Andrew Garfield'), ('Thor', 'Chris Hemsworth')} and type is <class 'set'>

Typecasting into Tuple

characters = ["Iron Man", "Thor", "Spiderman"]
real_names = ["RDJ", "Chris Hemsworth", "Andrew Garfield"]

result = zip(characters, real_names)

print(f"Tuple: {tuple(result)} and type is {type(tuple(result))}")

Output

Tuple: (('Iron Man', 'RDJ'), ('Thor', 'Chris Hemsworth'), ('Spiderman', 'Andrew Garfield')) and type is <class 'tuple'>

Unzipping the Values

We can actually unzip the values that were already zipped. Let’s see how to do so.

This can be done with the help of "*" (asterisk) operator.

Let’s see and understand with an example

characters = ["Iron Man", "Thor", "Spiderman"]
real_names = ["RDJ", "Chris Hemsworth", "Andrew Garfield"]
reel_names = ["Tony Stark", "Thor", "Peter Parker"]

mapping = zip(characters, real_names, reel_names)

mapped = list(mapping)

print("The zipped result is : ", end="")
print(mapped)

print("\n")

# unzipping values
char, real, reel = zip(*mapped)

print("The unzipped result: \n", end="")

print("The characters list is : ", end="")
print(char)

print("The real_names list is : ", end="")
print(real)

print("The reel_names list is : ", end="")
print(reel)

Output

The zipped result is : [('Iron Man', 'RDJ', 'Tony Stark'), ('Thor', 'Chris Hemsworth', 'Thor'), ('Spiderman', 'Andrew Garfield', 'Peter Parker')]


The unzipped result:
The characters list is : ('Iron Man', 'Thor', 'Spiderman')
The real_names list is : ('RDJ', 'Chris Hemsworth', 'Andrew Garfield')
The reel_names list is : ('Tony Stark', 'Thor', 'Peter Parker')

Notice how we unzipped the values of the variable mapped using "*" and to store the unzipped values we declared three variables char, real and reel

Conclusion

In this tutorial, you’ve learned to perform a parallel iteration using Python’s zip() function and I hope you understand how to use it.

You surely know how thezip() function works behind the scenes and creates an iterator of tuples.

Try the code snippets written above in your IDEs to understand the code better.

That’s all for now.

Keep Coding✌✌

Originally published at https://geekpython.in.

--

--

Sachin Pal
Sachin Pal

Written by Sachin Pal

I am a self-taught Python developer who loves to write on Python Programming and quite obsessed with Machine Learning.

No responses yet