89DEVs

Python zip() function

overview of zip()

The zip() function takes zero or more iterables and combines them into a tuple. One of the most common use cases is to combine two lists into a tuple.

use of zip()

The zip() function is most commonly used to combine two lists into a tuple. # use of zip() function numberList = [1, 2, 3, 4] stringList = ['one', 'two', 'three', 'four'] result = zip(numberList, stringList) print(list(result)) The numberList and the stringList are combined using the zip() function. Then the returned zip object is converted into a list and printed. It contains all elements of both lists packed into tuples. [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]

syntax of zip()

The syntax of the zip() function is: zip(*iterables)

arguments of zip()

The zip() function accepts zero or more iterables, which are used to combine them into a tuple. The iterables can be of type list, string, dict, or user-defined iterables.
argument required description
iterables optional the iterables to be combined into an tuple

return values of zip()

The zip() function returns an iterator based on the arguments provided to it. If no arguments are passed, an empty iterator is returned. The returned iterator can be used to convert it into another data structure. For example a list by using the list() function.

                
        

Summary


Click to jump to section