0

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?

1
  • 5
    Not sure why you think zip is "dirty", but zip(*tol) looks "cleaner". Commented Apr 13, 2016 at 7:49

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.