chongman chongman - 11 months ago 37
Python Question

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

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

Do you think it's be better to:

A) (assume that equal lengths is already checked)

for i in range(len(Latitudes):

*** EDIT
or, B)

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

**** EDIT (12/17) Note that B is incorrect. This gives me all the pairs, equivalent to itertools.product()

Any thoughts on the relative merits of each. I'm still getting in the hang of being "pythonic"


This is as pythonic as you can get:

for lat, long in zip(Latitudes, Longitudes):
    print lat, long