Skip to content

Instantly share code, notes, and snippets.

@carymrobbins
Created October 29, 2013 19:46
Show Gist options
  • Save carymrobbins/7221296 to your computer and use it in GitHub Desktop.
Save carymrobbins/7221296 to your computer and use it in GitHub Desktop.
Index function for iterables. Particularly handy when dealing with infinite iterables.
from itertools import islice
def iindex(iterable, index):
""" Indexing for all iterables. Translates to iterable[index]. """
return next(islice(iterable, index, index + 1))
def example():
def fibs():
x, y = 0, 1
yield x
yield y
while True:
x, y = y, x + y
yield y
# Get the 100th fibonacci number
iindex(fibs(), 100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment