freeCodeCamp/guide/english/python/zip-function/index.md

1.1 KiB

title
Python Zip Function

zip() is a built-in function in Python that returns a list of tuples. The nth tuple will have the nth element from each of the iterable arguments. If the arguments in the sequence are of unequal lengths, it will return a list truncated to the length of the shortest iterable.

Argument

Any number of iterables separated by comma.

Return Value

A list of tuple of nth element from all sequences

Code Sample

nums = [1,2,3,4]
print(*nums) # prints 1 2 3 4
numsAndNames = zip([1,2,3],['one','two','three'])
print(*numsAndNames) # prints (1,'one') (2,'two') (3,'three')

:rocket: Run Code

Official Docs - Python 3

Official Docs - Python 2.7