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

This is as pythonic as you can get: for lat, long in zip(Latitudes, Longitudes): print lat, long.

7 In Python 2. X you might consider itertools. Izip instead (zip does the same thing in Python 3.

X). – Nicholas Riley Dec 17 '09 at 2:08.

Good to see lots of love for zip in the answers here. However it should be noted that if you are using a python version before 3.0, the itertools module in the standard library contains an izip function which returns an iterable, which is more appropriate in this case (especially if your list of latt/longs is quite long). In python 3 and later zip behaves like izip.

Another way to do this would be to by using map. >>> a 1, 2, 3 >>> be 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 >>> be 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.

Iterating through elements of two lists simultaneously is known as zipping, and python provides a built in function for it, which is documented here. >>> x = 1, 2, 3 >>> y = 4, 5, 6 >>> zipped = zip(x, y) >>> zipped (1, 4), (2, 5), (3, 6) >>> x2, y2 = zip(*zipped) >>> x == list(x2) and y == list(y2) True Example is taken from pydocs In your case, it will be simply: for (lat, lon) in (latitudes, longitudes): ... process lat and lon.

In case your Latitude and Longitude lists are large and lazily loaded: from itertools import izip for lat, lon in izip(latitudes, longitudes): process(lat, lon) or if you want to avoid the for-loop from itertools import izip, imap out = imap(process, izip(latitudes, longitudes)).

However it should be noted that if you are using a python version before 3.0, the itertools module in the standard library contains an izip function which returns an iterable, which is more appropriate in this case (especially if your list of latt/longs is quite long). In python 3 and later zip behaves like izip.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions