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]