Itertools

accumulate(iterable, func, initial=null)

Add the elements of an iterable (or something that can be converted to an iterable) to produce a single value.

Arguments
  • iterable (Iterable()) – The iterable to accumulate over.

  • func (function()) – The function to use to combine the elements.

  • initial (number()) – The initial value to start with.

Returns

Generator – - A generator that yields the accumulated value.

chain(...iterables)

Chain together the results of several iterables.

Arguments
  • iterables (Iterable()) – The iterables to chain together.

Returns

Generator – - A generator that yields the results of the chained iterables.

combinations(iterable, r)

Return successive r-length combinations of elements in the iterable.

Arguments
  • iterable (Iterable()) – The iterable to combine over.

  • r (number()) – The number of elements to combine.

Returns

Generator – - A generator that yields the combinations.

combinations_with_replacement(iterable, r)

Return successive r-length combinations of elements in the iterable. The combinations can have duplicate elements.

Arguments
  • iterable (Iterable()) – The iterable to combine over.

  • r (number()) – The number of elements to combine.

Returns

Generator – - A generator that yields the combinations.

compress(data, selectors)

Return elements from an iterable as long as the predicate is true.

Arguments
  • data (Iterable()) – The iterable to filter.

  • selectors (Iterable()) – The iterable of booleans to filter by.

Returns

Generator – - A generator that yields the filtered elements.

count(start=0, step=1)

Return an infinite iterator of integers starting at start (or 0 if not provided), incremented by step (or 1 if not provided). step defaults to 1.

Arguments
  • start – The starting value (default 0).

  • step – The increment step (default 1).

Returns

An iterator of integers.

cycle(iterable)

Return an iterator that returns elements from the iterable and then repeats the same sequence forever.

Arguments
  • iterable (Iterable()) – The iterable to repeat.

Returns

Generator – - A generator that yields the repeated elements.

dropwhile(predicate, iterable)

Return an iterator that drops elements from the iterable as long as the predicate is true.

Arguments
  • predicate (function()) – The function to filter by.

  • iterable (Iterable()) – The iterable to filter.

filterfalse(predicate, iterable=null)

Return an iterator that filters elements from the iterable as long as the predicate is true.

Arguments
  • predicate (function()) – The function to filter by.

  • iterable (Iterable()) – The iterable to filter.

groupby(iterable, key=null)

Return an iterator that groups elements from the iterable into a sequence of tuples. The first element of each tuple is the key to the group. The second element of each tuple is the iterator of the group.

Arguments
  • iterable (Iterable()) – The iterable to group.

  • key (function()) – The function to group by.

Returns

Generator – - A generator that yields the grouped elements.

islice(iterable, start=0, stop, step=1)

Return an iterator that returns selected elements from the iterable.

Arguments
  • iterable (Iterable()) – The iterable to slice.

  • start (number()) – The starting index.

  • stop (number()) – The ending index.

  • step (number()) – The step size.

Returns

Generator – - A generator that yields the sliced elements.

pairwise(iterable)

Return an iterator that returns pairs of elements from the iterable.

Arguments
  • iterable (Iterable()) – The iterable to pair.

Returns

Generator – - A generator that yields the paired elements.

permutations(iterable, r=null)

Return an iterator that returns permutations of the elements from the iterable.

Arguments
  • iterable (Iterable()) – The iterable to permute.

  • r (number()) – The number of elements to permute.

Returns

Generator – - A generator that yields the permuted elements.

product(repeat=1, ...args)

Return an iterator that returns the cartesian product of the iterables.

Arguments
  • repeat (number()) – The number of times to repeat the product.

  • args (any()) – The iterables to product.

Returns

Generator – - A generator that yields the cartesian product.

repeat(object, times=null)

Return an iterator that returns the same element n times.

Arguments
  • object (any()) – The element to repeat.

  • times (number()) – The number of times to repeat the element.

starmap(func, iterable)

Return an iterator that returns the results of applying func to the elements from the iterable.

Arguments
  • func (function()) – The function to apply to the elements.

  • iterable (Iterable()) – The iterable to apply the function to.

Returns

Generator – - A generator that yields the results of applying func to the elements.

takewhile(predicate, iterable)

Return an iterator that returns elements from the iterable as long as the predicate is true.

Arguments
  • predicate (function()) – The predicate function.

  • iterable (Iterable()) – The iterable to take elements from.

Returns

Generator – - A generator that yields the elements from the iterable.

tee(iterable, n=2)

Return an iterator that returns n independent iterators of the iterable.

Arguments
  • iterable (Iterable()) – The iterable to tee.

  • n (number()) – The number of iterators to return.

Returns

Generator – - A generator that yields the iterators.

zip_longest(fillvalue=null, ...args)

Return an iterator that returns elements from the iterables. The iterator stops when the longest iterable is exhausted. If the iterables are of different lengths, missing values are filled with the fillvalue.

Arguments
  • fillvalue (Any()) – The fillvalue to use.

  • args (any()) – The iterables to zip.

Returns

Generator – - A generator that yields the zipped elements.