zip()

New built-in function: zip

  • zip(s1,s2,s3,...,sn)

  • Creates a list of tuples where each tuple contains an element from each si.
     a = [ 1,2,3 ]
     b = [ 10,11,12 ]
     c = zip(a,b)   # c = [ (1,10), (2,11), (3,12) ]
     

  • Resulting list is truncated to the length of the shortest sequence in s1,s2, ... sn.

  • Contrast to map(None,a,b)
     a = [1,2,3]
     b = [10,11,12,13]
     c = zip(a,b)       # c = [(1,10), (2,11), (3,12) ]
     d = map(None,a,b)  # d = [(1,10), (2,11), (3,12), (None,13) ]
     
<<< O'Reilly OSCON 2001, New Features in Python 2, Slide 15
July 26, 2001, beazley@cs.uchicago.edu
>>>