Better ways to get nth element from an unsubscriptable iterable

Viewed 5053

Sometimes an iterable might be not subscriptable. Say the return from itertools.permutations:

ps = permutations(range(10), 10)
print ps[1000]

Python will complain that 'itertools.permutations' object is not subscriptable

Of course one can perform next() by n times to get the nth element. Just wondering are there better ways to do so?

2 Answers

A more readable solution is :

next(x for i,x in enumerate(ps) if i==1000)
Related