Python Itertools Module: Cycle and Repeat

Use the itertools module. Invoke takewhile and other methods to implement advanced iteration logic.

Itertools. Iteration brings change, and in repetition, the lack of change. This logic can be expressed with imperative loops. But with itertools from Python, we can express iteration in a more elegant way.

Cycle generates an infinitely repeating series of values. It receives an iterable collection. And it repeats those elements (in a cycle) endlessly, with no concern for your feelings.

List: We pass a three-element list to cycle(). We then loop over the first ten elements of the result, which are 1, 2 and 3 repeated.

Caution: If you try to fully evaluate the result of cycle(), as by passing it to the list() built-in, your program will freeze.

Python program that uses cycle, itertools module import itertools # Cycle through these values. result = itertools.cycle([1, 2, 3]) # Display first ten results. i = 0 for value in result: print(value) i += 1 if i >= 10: break Output 1 2 3 1 2 3 1 2 3 1

Count. With count() we generate a count. We specify the start value (here it is zero) and then advance by a step. I specify the step 2, but using the default step of 1 is more common. A negative step too is possible.
Python program that uses count import itertools # Generate count from 0 to infinity, with step 2. result = itertools.count(0, 2) # Display until value 10. for value in result: print(value) if value >= 10: break Output 0 2 4 6 8 10

Repeat. The repeat method is simple. It receives two arguments. The first argument is the value you want to repeat. And the second argument is the number of times you wish to repeat that value.

Tip: We convert the result of the repeat method with the list built-in function. This yields a four-element list.

Python program that uses repeat import itertools # Repeat the value 5 four times. result = itertools.repeat(5, 4) print(list(result)) Output [5, 5, 5, 5]

Takewhile. This program uses takewhile. With takewhile, we continue "taking" elements from the start until one does not match the predicate condition. Here we specify the predicate as a lambda expression.

Note: We specify a lambda expression that returns true when the argument is less than 10. This is the first argument to takewhile.

And: The takewhile method returns an iterator that contains four elements, but not the fifth (10) because it is not less than 10.

Also: The final element, with value 1, is not included, because takewhile stopped processing after the first element that does not match.

Python program that uses takewhile, itertools import itertools # A list with seven values. values = [1, 5, 6, 8, 10, 12, 1] # Take values until one is higher than 9. result = itertools.takewhile(lambda v: v < 10, values) for value in result: print(value) Output 1 5 6 8

Dropwhile. With dropwhile(), we eliminate elements at the start of an iterable. The predicate condition (a lambda expression here) is evaluated. While the predicate evaluates to true, elements are removed.

Finally: When the first element that is not "dropped" is encountered, dropwhile() returns the remaining elements as an iterator.

Tip: The term "skip" is often used instead of "drop." Other that the term, the idea is the same.

Python program that uses dropwhile, itertools import itertools values = ["cat", "dog", "turnip", "carrot", "fish"] # Drop values while they are less than length 3. result = itertools.dropwhile(lambda s: len(s) <= 3, values) for value in result: print(value) Output turnip carrot fish

Permutations. A permutation treats differently-ordered values as a separate result. It generates all possible sequences within an argument. We pass the permutations() method an iterable argument.

And: We specify, with the second argument, the desired length of the results. Here we find 2-element permutations within the list.

Tip: Permutations are useful too in real programs. If you want to find all possible selections within a set of data, consider them.

Tuples: The permutations method returns a tuple of the data. If you specify a length of 2, it will return an iterator of 2-element tuples.

Python program that generates permutations import itertools values = [1, 2, 3] # Get all permutations of the three numbers. result = itertools.permutations(values, 2) for value in result: print(value) Output (1, 2) (1, 3) (2, 1) (2, 3) (3, 1) (3, 2)

Chain. Chain combines many iterable collections into a single one. Here we pass chain() three lists, and it returns one sequence of those values. Lists are not required. Chain uses a variable argument list.

So: The method can accept any number of arguments. Three lists are accepted, but so are fewer or more.

Def: tuple argument
Python program that uses chain, itertools import itertools values1 = [1, 2, 3, 4] values2 = [5, 6, 7, 8] values3 = [9, 10] # Chain three lists into one iterable. result = itertools.chain(values1, values2, values3) print(list(result)) Output [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Filterfalse. With filterfalse, we specify a predicate, often a lambda expression. Elements where the predicate evaluates to true are removed by filterfalse. So the predicate matches (and removes matching) elements.
Python program that uses filterfalse import itertools values = ["cat", "parrot", "dog", "bird"] # Filter out values with length greater than or equal to 4. result = itertools.filterfalse(lambda e: len(e) >= 4, values) for element in result: print(element) Output cat dog

Discussion. Is itertools worth using? With itertools we can write programs in a more functional way. We can specify what we want, and have itertools do the looping and processing for us—this is sometimes an improvement.

However: In my experience, imperative style, as with for-loops and statements, is often faster.

And: It is easier for other developers to understand. I prefer using for-loops and dictionaries in programs for these reasons.


Concept: Itertools enables us to use a functional language, like Haskell, in Python—with Python syntax.

Quote: This module implements a number of iterator building blocks inspired by constructs from APL, Haskell, and SML. Each has been recast in a form suitable for Python.


Summary. The methods available in itertools are powerful. A key is advantage too is their universality. You do not need to write them for each program. Instead, once you use a method once, you can use it easily in many programs.
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to