Python zip() function

In this tutorial, we will learn about the zip function in Python. Python’s zip() function is a versatile built-in tool that enables the combining of multiple iterables such as lists, strings, and more. The function takes any number of iterables as input parameters and aggregates them together by merging their elements element-wise.

One of the most common uses of the zip() function is to iterate through multiple iterables. When the zip() method is applied to the input iterables. It produces a zip object that is an iterator containing tuples. These tuples result from merging the elements of the input iterables. This pattern continues with the second element of each iterable and so on.

In essence, the zip python function maps the same index of various input iterables and converts them into a tuple. This allows for efficient and straightforward processing of data that has a one-to-one correspondence across multiple iterables. By using this powerful tool, Python programmers can write efficient and readable code that performs complex operations on multiple data sources with ease.

Let’s see what the zip function does in Python.

The return value of zip function

Python’s zip() method is a useful function that creates an iterator zipped object containing tuples. The tuples contain elements from the input iterables based on the number of input iterables passed. Understanding how this function behaves in different scenarios can help you efficiently handle multiple iterables.

  • When you pass no input iterables to the zip() method, it returns an empty iterator. If you pass only one iterable, the method returns an iterator of tuples, where each tuple contains a single element from the iterable.
  • When you pass multiple iterables, the zip() method returns tuples that contain elements from all the input iterables. However, keep in mind that the returned iterator size will be equal to the size of the smallest input iterable.
  • It’s important to note that the zip() method returns an iterator object rather than a list. This means that you can use it to efficiently iterate over multiple iterables without creating unnecessary lists or consuming too much memory. Additionally, you can use zip() in combination with other Python functions like map() and filter() to create powerful and efficient data processing pipelines.

Syntax of zip function Python


The * means that zip can take a variable number of iterables as arguments. Thus, zip(*iterables) means zip(iterable1, iterable2, iterable3, …).

Parameters of zip function in Python

Any number of built-in (list,Β dict,Β string) or user-defined iterables.

Example 1

countries = ["India", "UK", "USA", "Canada"]
capitals = ["Delhi", "London", "Washington DC", "Ottawa"]

country_capital = zip(countries, capitals)


Output :

<zip object at 0x7fce6586fb40>
[('India', 'Delhi'), ('UK', 'London'), ('USA', 'Washington DC'), ('Canada', 'Ottawa')]

Example 2

numbers = [1,2,3]
str_numbers = ['One','Two','Three']
roman_numbers = ['I','II','III']
result = zip(numbers, str_numbers, roman_numbers)

Output :

<zip object at 0x0000014EC4A90508>
[(1, 'One', 'I'), (2, 'Two', 'II'), (3, 'Three', 'III')]

Follow for More Info –

Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

Python Online Quiz

Level up your coding skills with our interactive programming quiz!