Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate through a list of tuples in Python? [duplicate]

coordinates = [(i, j) for i, row in enumerate(mymatrix) for j, v in enumerate(row) if v == '0']

I have a list of coordinates (tuples) and would like to print the coordinate, the nearest tuple to the right of the coordinate and the next tuple within the same column as the coordinate. How would I do this for each coordinate?

For example:

coordinates = [(0,0),(0,3),(1,0),(1,2),(1,3)]

Output for the first coordinate would be:

0 0 0 3 1 0

Output for the second coordinate would be:

0 3 -1 -1 1 3
like image 535
roshlife Avatar asked Sep 08 '25 12:09

roshlife


1 Answers

This print is not exactly as you defined, but you can use this kind of iteration on list of tuples and do whatever you need with each tuple :

coordinates = [(0,0),(0,3),(1,0),(1,2),(1,3)]
for (i,j) in coordinates:
    print i,j
like image 118
Gal Dreiman Avatar answered Sep 10 '25 03:09

Gal Dreiman