Python zip() function

(Sponsors) Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!

The zip()  function takes one or more sequences and combines the corresponding items in the sequences into a tuple. It stops when the shortest sequence is exhausted. In Python 2, zip()  returns an actual list which is not very efficient if you work with a large amount of data. For this reason, in Python 3, zip() returns an iterable which produces the result on demand.

Syntax: zip(iter1 [,iter2 [...]]) --> zip object

Python 3

To produce the result wrap the zip() in a list() call.

Python 2

Here is one practical example, where zip()  is used to iterate multiple sequences in parallel.

Here is another example where zip()  function is used to create a dictionary.

Other Tutorials (Sponsors)

This site generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!

Leave a Reply

Your email address will not be published. Required fields are marked *