Python: lists to tuples and tuples to lists
Get the code for this example The zip function in Python accepts a number of sequences (such as lists or arrays) and returns a list of tuples. The first element of each tuple comes from the first sequence, the second from the second sequence, and so on. For example: [code language=“Python”]a = range(0, 5) b = range(5, 10) c = range(10, 15) sequence_of_tuples = zip(a, b, c) print(sequence_of_tuples)[/code]
[(0, 5, 10), (1, 6, 11), (2, 7, 12), (3, 8, 13), (4, 9, 14)]I often use zipwhen I have a list ofxcoordinates and a list ofy coordinates, and I need to pass a list of (x,y) pairs to a function. The question is: what is the opposite of zip? How do you convert a sequence of tuples to multiple sequences? It turns out that zip is its own inverse, due to an obscure feature of the Python language called arbitrary argument lists. [code language=“Python”]a1, b1, c1 = zip(*sequence_of_tuples) print(a1) print(b1) print(c1)[/code]
Why does this work? The *in front of an argument to a function causes Python to unpack a list or tuple and pass the results as arguments to the function. Let’s see what arguments are actually being passed tozip: [code language=“Python”]def test_fn(*args): “““Number of arguments is not known in advance””” print(“Arguments passed to function:”) for a in args: print a test_fn(*sequence_of_tuples)[/code]
In our example, five tuples are passed to the function. zipreturns a list of tuples, where each tuple contains the i-th element from each of the argument sequences. Thus, the sequence of tuples is converted back to separate sequences. If you want lists instead of tuples, simply uselist to convert the tuple to a list: [code language=“Python”]a1 = list(a1)[/code] For a thorough discussion on the “opposite of zip”, see this thread.