# Is there a better way to iterate over two lists, getting one element from each list for each iteration?

### Question

I have a list of Latitudes and one of Longitudes and need to iterate over the latitude and longitude pairs.

Is it better to:

• A. Assume that the lists are of equal lengths:

``````for i in range(len(Latitudes):
Lat,Long=(Latitudes[i],Longitudes[i])
``````
• B. Or:

``````for Lat,Long in [(x,y) for x in Latitudes for y in Longitudes]:
``````

(Note that B is incorrect. This gives me all the pairs, equivalent to `itertools.product()`)

Any thoughts on the relative merits of each, or which is more pythonic?

1
129
7/10/2017 11:05:31 PM

This is as pythonic as you can get:

``````for lat, long in zip(Latitudes, Longitudes):
print lat, long
``````
246
12/17/2009 2:03:39 AM

Another way to do this would be to by using `map`.

``````>>> a
[1, 2, 3]
>>> b
[4, 5, 6]
>>> for i,j in map(None,a,b):
...   print i,j
...
1 4
2 5
3 6
``````

One difference in using map compared to zip is, with zip the length of new list is
same as the length of shortest list. For example:

``````>>> a
[1, 2, 3, 9]
>>> b
[4, 5, 6]
>>> for i,j in zip(a,b):
...   print i,j
...
1 4
2 5
3 6
``````

Using map on same data:

``````>>> for i,j in map(None,a,b):
...   print i,j
...

1 4
2 5
3 6
9 None
``````