Python Zip function- How to combine multiple iterable sequences?


In the computer, zipping files means combining multiple files into a single one. The zip() function in Python combines multiple iterable objects (strings, lists, etc.) into a single object. It returns a new iterable sequence in which each element at an index is the union of elements at the same index from each input sequence.

ZIP function combines two or more sequences into a single one.

How does the zip() function work?

  • In the zip function, one or more iterable objects are passed as arguments and return a list of tuples. Where individual tuple contains one element from each object passed into the function.
  • If no argument is passed, it returns an empty list (no tuples).
  • If a single argument is passed, the function converts the object to a list of tuples where each element is a tuple with a single element.

Syntax ->

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

Description->

Arguments (iterables) – These can be one or more built-in iterable (string, list, etc.) or be user-defined.
Value Returned(retVal) – Return a list of tuples.


How to use the zip function in a Python program?

The following is the primary example of how two lists of the same size will be zipped.

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

outcome = zip(list_1,list_2);

print(list(outcome));
[(100, 'century'), (200, 'double century')]

How to zip multiple sequences of different sizes?

We are passing three lists of different sizes in the following example. The output will consist of a list with the number of items equal to the smallest iterable passed in.

# 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));
[(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. The zip() function is used with the * operator and a zipped object. Please note that if the zip is created from sequences of different sizes, the unzip will not produce the original sequences. It will be a lossy unzip.

Unzipping with no loss: Following is an example where the input zipped list was created using sequences having the same number of elements.

# 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);
(100, 200, 0)
('century', 'double century', 'no runs')
('Player1', 'Player2', 'Player3')

Unzipping with loss: Following is an example where the input zipped list was created using sequences having a different number of elements.

# 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);
(100, 200, 0)
('century', 'double century', 'no runs')
('Player1', 'Player2', 'Player3')

When to use zip?

There can be multiple scenarios for using the inbuilt zip function. You can use zip to create a tuple as an element. 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.