chongman chongman - 1 year ago 66
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"

Answer Source

This is as pythonic as you can get:

for lat, long in zip(Latitudes, Longitudes):
    print lat, long
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download