# HG changeset patch # User Paul Boddie # Date 1480527719 -3600 # Node ID ea96958037c684cc0d49d5f6b9e8210bff298275 # Parent 88b64124852b8a372dc6215ddaad8e5bf7643f98 Implemented the zip function. diff -r 88b64124852b -r ea96958037c6 lib/__builtins__/iterable.py --- a/lib/__builtins__/iterable.py Wed Nov 30 18:41:04 2016 +0100 +++ b/lib/__builtins__/iterable.py Wed Nov 30 18:41:59 2016 +0100 @@ -69,6 +69,32 @@ def reversed(sequence): pass def sorted(iterable, cmp=None, key=None, reverse=False): pass def sum(sequence, start=0): pass -def zip(*args): pass + +def zip(args): + + """ + Zip the given 'args' together, producing for each index position tuples + containing the values for that position from each of the 'args'. + """ + + result = [] + pos = 0 + + # Repeat until one of the arguments runs out of elements. + + while True: + l = [] + + # Visit each argument in turn, collecting elements in the given + # position. + + for arg in args: + try: + l.append(arg[pos]) + except IndexError: + return result + + result.append(tuple(l)) + pos += 1 # vim: tabstop=4 expandtab shiftwidth=4