163

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?

0

7 Answers 7

308

This is as pythonic as you can get:

for lat, long in zip(Latitudes, Longitudes):
    print(lat, long)
6
  • 25
    In Python 2.x you might consider itertools.izip instead (zip does the same thing in Python 3.x). Dec 17, 2009 at 2:08
  • 2
    @NicholasRiley: care to mention why?
    – User
    Jan 25, 2013 at 22:58
  • 5
    It uses less memory and may be faster; it creates an iterator rather than an intermediate list. Jan 25, 2013 at 23:02
  • what if additionally I want the index i? Can I wrap that zip in enumerate? Mar 6, 2018 at 18:06
  • 1
    for index, (lat,long) in enumerate(zip(Latitudes, Longitudes)): Sep 24, 2018 at 14:39
53

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
2
  • is it possible to do it like: 14,15,16??
    – BomberMan
    Mar 3, 2015 at 11:55
  • 1
    The map-solution didn't work in python 3.6. I had to use itertools.zip_longest(a,b) for the same effect.
    – J_Scholz
    Jan 14, 2020 at 23:24
22

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.

15

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))
7

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 zip(latitudes, longitudes):
    ... process lat and lon
6
for Lat,Long in zip(Latitudes, Longitudes):
5

This post helped me with zip(). I know I'm a few years late, but I still want to contribute. This is in Python 3.

Note: in python 2.x, zip() returns a list of tuples; in Python 3.x, zip() returns an iterator. itertools.izip() in python 2.x == zip() in python 3.x

Since it looks like you're building a list of tuples, the following code is the most pythonic way of trying to accomplish what you are doing.

>>> lat = [1, 2, 3]
>>> long = [4, 5, 6]
>>> tuple_list = list(zip(lat, long))
>>> tuple_list
[(1, 4), (2, 5), (3, 6)]

Or, alternatively, you can use list comprehensions (or list comps) should you need more complicated operations. List comprehensions also run about as fast as map(), give or take a few nanoseconds, and are becoming the new norm for what is considered Pythonic versus map().

>>> lat = [1, 2, 3]
>>> long = [4, 5, 6]
>>> tuple_list = [(x,y) for x,y in zip(lat, long)]
>>> tuple_list
[(1, 4), (2, 5), (3, 6)]
>>> added_tuples = [x+y for x,y in zip(lat, long)]
>>> added_tuples
[5, 7, 9]

Not the answer you're looking for? Browse other questions tagged or ask your own question.