Python Zip function- How to combine multiple iterable sequences?


With the zip() function you can create a new iterable sequence in which each element at an index will be the union of elements at the same index in each sequence.

ZIP function combines two or more sequences into a single one

How to use the zip function?

  • The zip function takes zero or more iterable objects and returns an iterable list of tuples. Each tuple has one element from each object passed-in function.
  • If no argument is passed, returns an empty list with zero tuples.
  • If a single object is passed, the function converts, the object to the list of tuples. Where each element is a tuple with a single element.

Syntax ->

zip(* iterables) or zip(iterable1, iterable2, ...iterableN);

Arguments – Can be one or more build-in iterables (string, list, etc.) or they could be user-defined.
Return Value – Return a list of tuples.


How to use the zip function in a Python program?

This is the basic example, where two lists of the same size will combine.

# Program to combine two lists 
list_1 = [100,200];
list_2 = ["century", "double century"];

outcome = zip(list_1,list_2);

print(list(outcome));

Output->

[(100, 'century'), (200, 'double century')]

Multiple lists of different sizes –

In this example, we are passing three lists of different sizes. The output will be a list with the number of items equal to the size of the smallest iterable passed in arguments.

# Program to combine three lists of different sizes
list_1 = [100,200, 0];
list_2 = ["century", "double century", "no runs"];

list_3 = ["Player1", "Player2", "Player3", "Player4"];

outcome = zip(list_1,list_2,list_3);

print(list(outcome));

Output->

[(100, 'century', 'Player1'), (200, 'double century', 'Player2'), (0, 'no runs', 'Player3')]

How to get iterable sequences back from a zipped Object?

The process is unzipping a zipped object. For that zip() function is used with the * operator along with a zipped object. Please note that, if the zip is created from sequences of different sizes then the unzip will not produce the original sequences. It will be a lossy unzip.

# Program to zip three lists of same sizes
list_1 = [100,200, 0];
list_2 = ["century", "double century", "no runs"];

list_3 = ["Player1", "Player2", "Player3"];
#zip 
outcome = zip(list_1,list_2,list_3);
#unzip 
x,y,z = zip(*outcome);

print(x);
print(y);
print(z);

Output->

(100, 200, 0)
('century', 'double century', 'no runs')
('Player1', 'Player2', 'Player3')
# Program to zip three lists of different sizes
list_1 = [100,200, 0];
list_2 = ["century", "double century", "no runs"];

list_3 = ["Player1", "Player2", "Player3", "Player4"];
#zip 
outcome = zip(list_1,list_2,list_3);
#unzip 
x,y,z = zip(*outcome);

print(x);
print(y);
print(z);

Output->

(100, 200, 0)
('century', 'double century', 'no runs')
('Player1', 'Player2', 'Player3')

When to use zip?

There can be multiple scenarios to use the zip inbuilt function. Whenever you need to create a tuple as an element you can use zip. We can use the zip function to create a dictionary from the lists of keys and values. Or you can use it in for loop to iterate multiple sequences.