C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
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
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
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]
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
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
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.
TuplePython 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)
So: The method can accept any number of arguments. Three lists are accepted, but so are fewer or more.
Def: tuple argumentPython 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]
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
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.
DictionaryForConcept: 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.
Itertools: python.org