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:
break1
2
3
1
2
3
1
2
3
1

import itertools
*# Generate count from 0 to infinity, with step of 2.*
result = itertools.count(0, 2)
*# Display until value 10.*
for value in result:
print(value)
if value >= 10: break0
2
4
6
8
10

import itertools
*# Repeat the value 5 four times.*
result = itertools.repeat(5, 4)
print(list(result))[5, 5, 5, 5]

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)1
5
6
8

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)turnip
carrot
fish

Tuple

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

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))[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

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)cat
dog

Dictionary

for

Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.

Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.

This page was last updated on Jun 3, 2022 (simplify).