itertools Module

Advanced iteration utilities

Interview Relevant: Efficient iteration

itertools

Standard library for efficient looping.

Code Examples

python
1import itertools
2
3# Infinite count
4for i in itertools.count(10):
5    if i > 12: break
6    print(i)
7
8# Chain
9list(itertools.chain([1, 2], [3, 4]))
10
11# Product (Cartesian)
12list(itertools.product('AB', [1, 2])) 
13# [('A', 1), ('A', 2), ('B', 1), ('B', 2)]

AI Tutor

Ask about the topic

Sign in Required

Please sign in to use the AI tutor

Sign In