Problem: You need to convert a list like [ “x”, 1, “y”, 2 ] to [ (“x”, 1), (“y”, 2) ]. This is input from a legacy system. Chiman on python.fi IRC channel come up with a neat solution.
>>> a = [1, 2, 3, 4] >>> zip(*[a[x::2] for x in (0, 1)]) [(1, 2), (3, 4)]
In readability wise this might not be the optimal solution, so alternative ideas are also welcome 🙂
Also here is a generic solution where you can set the length of a tuple.
Is “tuplyfying” English….?
 Subscribe to RSS feed
Follow me on Twitter
Follow me on Facebook
Follow me Google+