Question
Asked By – chongman
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?
Now we will see solution for issue: Is there a better way to iterate over two lists, getting one element from each list for each iteration? [duplicate]
Answer
This is as pythonic as you can get:
for lat, long in zip(Latitudes, Longitudes):
print(lat, long)
This question is answered By – Roberto Bonvallet
This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0