Skip to content

Instantly share code, notes, and snippets.

@mariocesar
Last active August 29, 2015 14:13
Show Gist options
  • Save mariocesar/427fa1f25d49168c2296 to your computer and use it in GitHub Desktop.
Save mariocesar/427fa1f25d49168c2296 to your computer and use it in GitHub Desktop.
Python Itertools Notes

Itertools Notes

from itertools import *

Infinite Iterators

count(start, [step])

Return a count object where on every .next() call will return start + step, start * 1 + step, start * 2 + step ...

count()         # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ... ]
count(10)       # [10, 11, 12, 13, 14, 15, 16, 17, 18 ... ]
count(10, 2)    # [10, 12, 14, 16, 18, 20, 22, 24, 26 ... ]
count(10, -2)   # [10, 8, 6, 4, 2, 0, -2, -4, -6, -8 ... ]

cycle(iterable)

Return the sequence of the iterable until the end and then repeat indefinitely.

cycle([0, 0, 1])    # [0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0 ... ]
cycle([1, 2, 3])    # [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2 ... ]
cycle([1])          # [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 ... ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment