Python zip() Function Examples

In this Python tutorial, we will discuss how to use Python zip() function with various examples.

Python’s built-in zip() function is a powerful function for combining two or more iterables into a single iterable.

Python zip() function

The Python zip() function returns a zip object, and the zip() function takes iterables, aggregates them in a tuple, and returns it.

Syntax of zip() function in Python:

The syntax of the Python zip() function is like below:

zip(*iterables)

Examples of Python Zip()

Now, let us see a few examples of how to use the python zip() function.

Example-1: Python zip() function

This is the most basic usage of the zip() function, where you pass two iterables (in this case, two lists) to the function, and it returns an iterator that produces tuples containing elements from each of the input iterables, until the shortest iterable is exhausted.

# Create a list of numbers
numbers = [1, 2, 3]

# Create a list of strings representing countries
countries = ['USA', 'United Kingdom', 'canada']

# Zip the two lists together into pairs
zipped = zip(numbers, countries)

# Loop over each pair in the zipped list
for pair in zipped:
    # Print the pair
    print(pair)

The above code defines two lists, numbers, and countries. numbers contain three integers, while countries contain three strings representing the names of countries.

  • The zip() function is then used to create a new list, zipped, which contains pairs of corresponding elements from the numbers and countries lists.
  • Finally, a for loop is used to iterate over each pair in the zipped list and print it to the console.
Output: (1, 'USA')
(2, 'United Kingdom')
(3, 'canada')

Example-2: Python zip unpacking zip objects

When you use the zip() function to iterate over multiple iterables in parallel, you can unpack the resulting tuples into separate variables, which allows you to access each element individually.

# Create a list of numbers
numbers = [1, 2, 3]

# Create a list of strings representing countries
countries = ['USA', 'United Kingdom', 'canada']

# Zip the two lists together into pairs
zipped = zip(numbers, countries)

# Loop over each pair in the zipped list
for num, country in zipped:
    # Unpack the pair into variables num and country
    # Print the variables
    print(num, country)

In the above code, we create two lists called numbers and countries. numbers contain three integer values, while countries contain three string values.

  • Next, the zip() function is used to create a new list called zipped that contains pairs of corresponding elements from the numbers and countries lists. In this case, each pair will consist of a number from the numbers list and a country name from the countries list.
  • Next, we use a for loop to iterate over each pair in the zipped list. The loop is using tuple unpacking to assign the first element of each pair to the variable num and the second element to the variable country.
  • During each iteration of the loop, the print() function is called to output the values of num and country to the console.
1 USA
2 United Kingdom
3 canada

Example-3: Python zip unzipping a zipped object

If you have a zipped object (i.e., an iterator returned by the zip() function), you can use the zip() function again to unzip it back into separate iterables. To do this, you use the * operator to unpack the zipped object into separate arguments for the zip() function.

# Create a list of numbers
numbers = [1, 2, 3]

# Create a list of strings representing countries
countries = ['USA', 'United Kingdom', 'canada']

# Zip the two lists together into pairs
zipped = zip(numbers, countries)

# Unzip the pairs back into separate lists using the * operator
nums, countrs = zip(*zipped)

# Print the resulting lists
print(nums)
print(countrs)

The above code has two lists numbers and countries, containing integer and string values, respectively.

  • The code then uses the zip() function to create a new list zipped that contains pairs of corresponding elements from numbers and countries.
  • The pairs are then unpacked from the zipped list into separate lists nums and countrs using the zip() function again with the * operator. Finally, the resulting lists nums and countrs are printed to the console.
Output: (1, 2, 3)
('USA', 'United Kingdom', 'canada')

Example-4: Python zip with *args

This method uses the *args syntax with zip() to zip an arbitrary number of iterables that are passed as separate arguments to a function. It can be useful when you want to work with a variable number of iterables, such as when you are creating a function that accepts a flexible number of arguments.

# Define a function named "combine" that takes any number of lists as input
def combine(*args):
    # Use the zip function to combine the input lists into a single list of tuples
    zipped = zip(*args)
    # Use a list comprehension to create a new list where each element is the sum of the corresponding elements in each tuple
    return [sum(pair) for pair in zipped]

# Call the combine function with three input lists
result = combine([1, 2, 3], [4, 5, 6], [7, 8, 9])

# Print the resulting list
print(result)

The above code defines a function named combine() that takes any number of lists as input arguments.

  • Inside the function, the zip() function is used to combine the input lists into a single list of tuples where each tuple contains the corresponding elements from each input list.
  • Then a list comprehension is used to create a new list where each element is the sum of the corresponding elements in each tuple.
  • Finally, the resulting list is returned by the combine() function.
Output: [12, 15, 18]

Example-5: Python unzipping the Value Using zip()

Now, we will see python unzipping the Value Using zip()

The * operator is used in conjunction with zip() to unzip the list.

Example:

val1 = ['a', 'b', 'c']
val2 = [1, 2, 3]
res = zip(val1, val2)
res_list = list(res)
print(res_list)
v1, v2 =  zip(*res_list)
print('v1 =', v1)
print('v2 =', v2)

You can refer to the below screenshot to see the output for python unzipping the Value Using zip().

Python unzipping the Value Using zip()
Python unzipping the Value Using zip()

You may like, How to create a string in Python?

Example-6: Python zip different length

Let’s see an example of a python zip with different lengths.

  • In this example, we have defined iterators of different lengths and the first elements of all of them are joined together. Similarly, the second, third, and fourth elements of them are joined together.
  • But there are no fifth elements, so the remaining iterators are not included in the output.

Example:

list1 = ['Rohan', 'Preeti', 'Kislay', 'Ankita']
list2 = ['Red', 'Blue', 'Green', 'Pink', 'Black']
list3 = ['101', '201', '301', '401', '501']
result = zip(list1, list2, list3)
print(list(result))

You can refer to the below screenshot to see the output for python zip different length

Python zip different length
Python zip different length

Example-7: Python zip object

Now, we will see python zip object

In this example, it returns an iterator of tuples for the specified lists. Python zip object works as an iterator and returns zip object.

Example:

val1 = [101,102,103]
val2 = ['One','Two','Three']
result = zip(val1, val2)
print(result)
print(list(result))

You can refer to the below screenshot to see the output for python zip object.

Python zip object
Python zip object

You may like the following Python tutorials:

In this Python tutorial, we have learned about the Python zip() method and syntax of zip() in python and also we cover a few examples of the Python zip() method.