DEV Community

Cover image for What Is zip() Function In Python
Sachin
Sachin

Posted on • Updated on • Originally published at geekpython.in

What Is zip() Function In Python

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))
Enter fullscreen mode Exit fullscreen mode

Output

(('Computer', 3), ('Keyboard', 2), ('CPU', 4), ('Mouse', 6))
Enter fullscreen mode Exit fullscreen mode

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)))
Enter fullscreen mode Exit fullscreen mode

Output

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

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.

We will better understand through the illustration below

zip function working

  • We 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))
Enter fullscreen mode Exit fullscreen mode

Output

[('Python', 1991), ('JavaScript', 1995), ('C', 1972)]
Enter fullscreen mode Exit fullscreen mode

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 with an example

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

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

print(list(mapping))
Enter fullscreen mode Exit fullscreen mode

Output

Traceback (most recent call last):
  ....
ValueError: zip() argument 2 is shorter than argument 1
Enter fullscreen mode Exit fullscreen mode

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

The strict argument is added in Python version 3.10

If the iterables have different lengths and we use the strict argument, 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))
Enter fullscreen mode Exit fullscreen mode

Output

[]
Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

Output

[('Python',), ('JavaScript',), ('C',), ('C++',)]
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

Output

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

Example: Using range() function

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

values = zip(range(4), name)

print(tuple(values))
Enter fullscreen mode Exit fullscreen mode

Output

((0, 'Sachin'), (1, 'Rishu'), (2, 'Yashwant'), (3, 'Abhishek'))
Enter fullscreen mode Exit fullscreen mode

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))
Enter fullscreen mode Exit fullscreen mode

Output

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

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))}")
Enter fullscreen mode Exit fullscreen mode

Output

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

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))}")
Enter fullscreen mode Exit fullscreen mode

Output

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

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))}")
Enter fullscreen mode Exit fullscreen mode

Output

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

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))}")
Enter fullscreen mode Exit fullscreen mode

Output

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

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 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(f"Zipped result: {mapped}", end="")

print("\n")

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

print("Unzipped result:")

print(f"The characters list is : {char}")

print(f"The real_names list is : {real}")

print(f"The reel_names list is : {reel}")
Enter fullscreen mode Exit fullscreen mode

Output

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

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')
Enter fullscreen mode Exit fullscreen mode

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 now understand how the zip() function works behind the scenes to generate a tuple iterator.

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


πŸ†Other articles you might be interested in if you liked this one

βœ…How to convert bytes into strings in Python?

βœ…f-string: An improved and modern way of string formatting.

βœ…How to access the list values within the dictionary in Python?

βœ…How to use __str__ and __repr__ to change the string representation of the objects in Python?

βœ…How do sort() and sorted() functions are different from each other?

βœ…What is the difference between seek() and tell() in Python?

βœ…How to use tempfile module to create temporary files and dirs in Python?


That's all for now.

Keep Coding✌✌

Top comments (0)