Why create an actual list with a range?
Yes, xrange(0, len(x), 2)
will work more efficiently with memory.
Why not use itertools.izip () in solution 2?
Yes, zip () creates an actual list, so you can save memory using itertools.izip.
Does it really matter?
Differences in speed may be small. Memory efficiency improves speed only when data exceeds the size of memory caches. Some of the benefits are offset by the overhead of iterators.
Since the dictionary stores keys and values, the only memory is stored in tuples pointing to keys and values. Thus, the savings in this situation are much more modest than for other iterator applications that do not accumulate all the results in the container.
So this is probably "a lot of noise from nothing."
What about Python 3?
In Python 3, range () and zip () return iterators.
source share