Light Yagmi
Light Yagmi

Reputation: 5235

loop for tuple of list on python

I have tuple of lists. The lengths of the lists are same each other. For example:

tol = ([1,2,3], [4,5,6])

I'd like to loop all pair of two lists like:

for v1, v2 in some_operation(tol):
    print "(%f, %f)" % (v1, v2)

The above code should print (1,4)\n(2,5)\n(3,6)\n.

One (little dirty) way is use zip

for v1, v2 in zip(tol[0], tol[1]):
    print...

Could you show me more simple way?

Upvotes: 0

Views: 50

Answers (2)

Mani
Mani

Reputation: 949

Using Zip would be simple and clean

tol = ([1,2,3], [4,5,6])

for v1, v2 in zip(*tol):
    print "(%d, %d)" % (v1, v2)

As you expect the output would be (1,4)\n(2,5)\n(3,6)\n.

Upvotes: 2

smead
smead

Reputation: 1808

for v in zip(*tol):
    print "(%f, %f)" % (v[0], v[1])

Upvotes: 1

Related Questions