Python Tutorial

Sunday, October 7, 2012

Python keyword: zip


Python zip keoword return 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.

 
a=[1,2,3]
b=[4,5,6]

z=zip(a,b)
print z

ai,bi=zip(*z)
print ai
print bi

print "-"*25

a=[1,2]
b=[4,5,6]

z=zip(a,b)
print z

ai,bi=zip(*z)
print ai
print bi

print "."*25

a=[1,2,4]
b=[4,5]

z=zip(a,b)
print z

ai,bi=zip(*z)
print ai
print bi


Output:
[(1, 4), (2, 5), (3, 6)]
(1, 2, 3)
(4, 5, 6)
-------------------------
[(1, 4), (2, 5)]
(1, 2)
(4, 5)
.........................
[(1, 4), (2, 5)]
(1, 2)
(4, 5)

1 comments:

Post a Comment