I've got a large iterable, in fact, a large iterable given by:
itertools.permutations(range(10))
I would like to access to the millionth element. I alredy have problem solved in some different ways.
Casting iterable to list and getting 1000000th element:
return list(permutations(range(10)))[999999]
Manually skiping elements till 999999:
p = permutations(range(10)) for i in xrange(999999): p.next() return p.next()
Manually skiping elements v2:
p = permutations(range(10)) for i, element in enumerate(p): if i == 999999: return element
Using islice from itertools:
return islice(permutations(range(10)), 999999, 1000000).next()
But I still don't feel like none of them is the python's elegant way to do that. First option is just too expensive, it needs to compute the whole iterable just to access a single element. If I'm not wrong, islice does internally the same computation I just did in method 2, and is almost exactly as 3rd, maybe it has even more redundant operations.
So, I'm just curious, wondering if there is in python some other way to access to a concrete element of an iterable, or at least to skip the first elements, in some more elegant way, or if I just need to use one of the aboves.