Tutorial by Examples

Start with an iterable which needs to be grouped lst = [("a", 5, 6), ("b", 2, 4), ("a", 2, 5), ("c", 2, 6)] Generate the grouped generator, grouping by the second element in each tuple: def testGroupBy(lst): groups = itertools.groupby(lst, key=lambda...
Itertools "islice" allows you to slice a generator: results = fetch_paged_results() # returns a generator limit = 20 # Only want the first 20 results for data in itertools.islice(results, limit): print(data) Normally you cannot slice a generator: def gen(): n = 0 wh...
This function lets you iterate over the Cartesian product of a list of iterables. For example, for x, y in itertools.product(xrange(10), xrange(10)): print x, y is equivalent to for x in xrange(10): for y in xrange(10): print x, y Like all python functions that accept a v...
Introduction: This simple function generates infinite series of numbers. For example... for number in itertools.count(): if number > 20: break print(number) Note that we must break or it prints forever! Output: 0 1 2 3 4 5 6 7 8 9 10 Arguments: count() ta...
itertools.takewhile enables you to take items from a sequence until a condition first becomes False. def is_even(x): return x % 2 == 0 lst = [0, 2, 4, 12, 18, 13, 14, 22, 23, 44] result = list(itertools.takewhile(is_even, lst)) print(result) This outputs [0, 2, 4, 12, 18]. Not...
itertools.dropwhile enables you to take items from a sequence after a condition first becomes False. def is_even(x): return x % 2 == 0 lst = [0, 2, 4, 12, 18, 13, 14, 22, 23, 44] result = list(itertools.dropwhile(is_even, lst)) print(result) This outputs [13, 14, 22, 23, 44]. ...
Similar to the built-in function zip(), itertools.zip_longest will continue iterating beyond the end of the shorter of two iterables. from itertools import zip_longest a = [i for i in range(5)] # Length is 5 b = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] # Length is 7 for i in zip_longest(a, b): x...
itertools.combinations will return a generator of the k-combination sequence of a list. In other words: It will return a generator of tuples of all the possible k-wise combinations of the input list. For Example: If you have a list: a = [1,2,3,4,5] b = list(itertools.combinations(a, 2)) print ...
Use itertools.chain to create a single generator which will yield the values from several generators in sequence. from itertools import chain a = (x for x in ['1', '2', '3', '4']) b = (x for x in ['x', 'y', 'z']) ' '.join(chain(a, b)) Results in: '1 2 3 4 x y z' As an alternate constructo...
Repeat something n times: >>> import itertools >>> for i in itertools.repeat('over-and-over', 3): ... print(i) over-and-over over-and-over over-and-over
Python 3.x3.2 accumulate yields a cumulative sum (or product) of numbers. >>> import itertools as it >>> import operator >>> list(it.accumulate([1,2,3,4,5])) [1, 3, 6, 10, 15] >>> list(it.accumulate([1,2,3,4,5], func=operator.mul)) [1, 2, 6, 24, 120] ...
cycle is an infinite iterator. >>> import itertools as it >>> it.cycle('ABCD') A B C D A B C D A B C D ... Therefore, take care to give boundaries when using this to avoid an infinite loop. Example: >>> # Iterate over each element in cycle for a fixed range >&g...
itertools.permutations returns a generator with successive r-length permutations of elements in the iterable. a = [1,2,3] list(itertools.permutations(a)) # [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)] list(itertools.permutations(a, 2)) [(1, 2), (1, 3), (2, 1), (2, 3), (3...

Page 1 of 1