I'm trying to solve Project Euler's problem 40. I generated a string of concatenated integers with

iteration = (i for i in ''.join(map(str, (i for i in xrange(1,10**6)))))

An now i want to get elements with indexes 1, 10, 100, 1000, 10000, 100000, 1000000 counting from 1. I couldn't use islice here, because every call to next shifts current value to yield to the right. For example

next(islice(iteration, 1, 2)) + next(islice(iteration, 3, 4))

produces '26' instead of '24'.

Update (25.11.12, 4:43 UTC+0):

Thanks for all the suggestions. My current code looks like:

it = (i for i in ''.join(map(str, (i for i in xrange(1,10**6)))))
ds = [int(nth(it, 10**i-10**(i-1)-1)) for i in range(7)]
return product(ds)

The ugly argument for nth is to generate a sequence of 0, 8, 89, 899, 8999 etc.

Alternatively, you could use tee and use nth with a different iterator each time. This way you don't have to worry about the fact that your single iterator is getting consumed. On the other hand you might start swallowing memory if your iterators are very long.