Search code examples
pythontransposelist-manipulationbuilt-in

Is there a python builtin to create tuples from multiple lists?


Is there a python builtin that does the same as tupler for a set of lists, or something similar:

def tupler(arg1, *args):
    length = min([len(arg1)]+[len(x) for x in args])
    out = []
    for i in range(length):
        out.append(tuple([x[i] for x in [arg1]+args]))
    return out

so, for example:

tupler([1,2,3,4],[5,6,7])

returns:

[(1,5),(2,6),(3,7)]

or perhaps there is proper pythony way of doing this, or is there a generator similar???


Solution

  • I think you're looking for zip():

    >>> zip([1,2,3,4],[5,6,7])
    [(1, 5), (2, 6), (3, 7)]