首页 > 代码库 > [Python] zip()

[Python] zip()

zip() is a built-in function.

zip([iterable, ...])

  

This function returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The returned list is truncated in length to the length of the shortest argument sequence. When there are multiple arguments which are all of the same length, zip() is similar to map() with an initial argument of None. With a single sequence argument, it returns a list of 1-tuples.

 

Ex:

Input:

X = [1,2,3]
Y = [4,5,6]
zipper = zip(X, Y)

  

Output:

zipper = [(1,4), (2,5), (3,6)]

  

Inversely, we can

x2, y2 = zip(*zipper)

  

x2 == X

y2 == Y

[Python] zip()