Linux ip-148-66-134-25.ip.secureserver.net 3.10.0-1160.119.1.el7.tuxcare.els11.x86_64 #1 SMP Sun Nov 3 09:06:59 UTC 2024 x86_64
Apache
: 148.66.134.25 | : 18.117.154.229
66 Domain
8.0.30
amvm
www.github.com/MadExploits
Terminal
AUTO ROOT
Adminer
Backdoor Destroyer
Linux Exploit
Lock Shell
Lock File
Create User
CREATE RDP
PHP Mailer
BACKCONNECT
UNLOCK SHELL
HASH IDENTIFIER
CPANEL RESET
BLACK DEFEND!
README
+ Create Folder
+ Create File
/
usr /
share /
doc /
python-docs-2.7.5 /
html /
library /
[ HOME SHELL ]
Name
Size
Permission
Action
2to3.html
49.27
KB
-rw-r--r--
__builtin__.html
10.26
KB
-rw-r--r--
__future__.html
13.79
KB
-rw-r--r--
__main__.html
7.05
KB
-rw-r--r--
_winreg.html
59.21
KB
-rw-r--r--
abc.html
23.9
KB
-rw-r--r--
aepack.html
13.16
KB
-rw-r--r--
aetools.html
14.91
KB
-rw-r--r--
aetypes.html
18.88
KB
-rw-r--r--
aifc.html
22.4
KB
-rw-r--r--
al.html
17.34
KB
-rw-r--r--
allos.html
33.72
KB
-rw-r--r--
anydbm.html
16.33
KB
-rw-r--r--
archiving.html
9.26
KB
-rw-r--r--
argparse.html
237.62
KB
-rw-r--r--
array.html
29.29
KB
-rw-r--r--
ast.html
34.98
KB
-rw-r--r--
asynchat.html
31.43
KB
-rw-r--r--
asyncore.html
36.51
KB
-rw-r--r--
atexit.html
16.8
KB
-rw-r--r--
audioop.html
31.36
KB
-rw-r--r--
autogil.html
8.19
KB
-rw-r--r--
base64.html
19.67
KB
-rw-r--r--
basehttpserver.html
34.04
KB
-rw-r--r--
bastion.html
11.04
KB
-rw-r--r--
bdb.html
36.68
KB
-rw-r--r--
binascii.html
20.67
KB
-rw-r--r--
binhex.html
10.58
KB
-rw-r--r--
bisect.html
23.24
KB
-rw-r--r--
bsddb.html
26.43
KB
-rw-r--r--
bz2.html
26.08
KB
-rw-r--r--
calendar.html
37.79
KB
-rw-r--r--
carbon.html
48.94
KB
-rw-r--r--
cd.html
27.96
KB
-rw-r--r--
cgi.html
49.92
KB
-rw-r--r--
cgihttpserver.html
13.1
KB
-rw-r--r--
cgitb.html
11.41
KB
-rw-r--r--
chunk.html
14.66
KB
-rw-r--r--
cmath.html
25.63
KB
-rw-r--r--
cmd.html
26.09
KB
-rw-r--r--
code.html
24.58
KB
-rw-r--r--
codecs.html
100.64
KB
-rw-r--r--
codeop.html
14.84
KB
-rw-r--r--
collections.html
133.96
KB
-rw-r--r--
colorpicker.html
7.52
KB
-rw-r--r--
colorsys.html
11.04
KB
-rw-r--r--
commands.html
14.36
KB
-rw-r--r--
compileall.html
16.83
KB
-rw-r--r--
compiler.html
67.75
KB
-rw-r--r--
configparser.html
62.13
KB
-rw-r--r--
constants.html
12.83
KB
-rw-r--r--
contextlib.html
19.39
KB
-rw-r--r--
cookie.html
39.07
KB
-rw-r--r--
cookielib.html
83.82
KB
-rw-r--r--
copy.html
12.19
KB
-rw-r--r--
copy_reg.html
13.76
KB
-rw-r--r--
crypt.html
10.04
KB
-rw-r--r--
crypto.html
7.59
KB
-rw-r--r--
csv.html
67.37
KB
-rw-r--r--
ctypes.html
238.78
KB
-rw-r--r--
curses.ascii.html
22.29
KB
-rw-r--r--
curses.html
146.63
KB
-rw-r--r--
curses.panel.html
14.39
KB
-rw-r--r--
custominterp.html
7.62
KB
-rw-r--r--
datatypes.html
16.84
KB
-rw-r--r--
datetime.html
226.59
KB
-rw-r--r--
dbhash.html
15.48
KB
-rw-r--r--
dbm.html
12.07
KB
-rw-r--r--
debug.html
10.15
KB
-rw-r--r--
decimal.html
194.44
KB
-rw-r--r--
development.html
14.17
KB
-rw-r--r--
difflib.html
84.83
KB
-rw-r--r--
dircache.html
11.41
KB
-rw-r--r--
dis.html
69.95
KB
-rw-r--r--
distutils.html
8.05
KB
-rw-r--r--
dl.html
16.33
KB
-rw-r--r--
doctest.html
165.54
KB
-rw-r--r--
docxmlrpcserver.html
16.43
KB
-rw-r--r--
dumbdbm.html
14.02
KB
-rw-r--r--
dummy_thread.html
9.43
KB
-rw-r--r--
dummy_threading.html
8.37
KB
-rw-r--r--
easydialogs.html
30.55
KB
-rw-r--r--
email-examples.html
45.65
KB
-rw-r--r--
email.charset.html
26.8
KB
-rw-r--r--
email.encoders.html
11.86
KB
-rw-r--r--
email.errors.html
15.77
KB
-rw-r--r--
email.generator.html
20.77
KB
-rw-r--r--
email.header.html
26.92
KB
-rw-r--r--
email.html
44.24
KB
-rw-r--r--
email.iterators.html
11.52
KB
-rw-r--r--
email.message.html
63.16
KB
-rw-r--r--
email.mime.html
27.93
KB
-rw-r--r--
email.parser.html
30.45
KB
-rw-r--r--
email.util.html
24.46
KB
-rw-r--r--
errno.html
37.99
KB
-rw-r--r--
exceptions.html
56.13
KB
-rw-r--r--
fcntl.html
22.67
KB
-rw-r--r--
filecmp.html
22.3
KB
-rw-r--r--
fileformats.html
9.14
KB
-rw-r--r--
fileinput.html
24.28
KB
-rw-r--r--
filesys.html
10.2
KB
-rw-r--r--
fl.html
49.92
KB
-rw-r--r--
fm.html
11.91
KB
-rw-r--r--
fnmatch.html
14.58
KB
-rw-r--r--
formatter.html
34.06
KB
-rw-r--r--
fpectl.html
16.01
KB
-rw-r--r--
fpformat.html
10.59
KB
-rw-r--r--
fractions.html
22.61
KB
-rw-r--r--
framework.html
33.34
KB
-rw-r--r--
frameworks.html
7.14
KB
-rw-r--r--
ftplib.html
43.99
KB
-rw-r--r--
functions.html
183.14
KB
-rw-r--r--
functools.html
27.17
KB
-rw-r--r--
future_builtins.html
13.04
KB
-rw-r--r--
gc.html
25.75
KB
-rw-r--r--
gdbm.html
15.96
KB
-rw-r--r--
gensuitemodule.html
11.51
KB
-rw-r--r--
getopt.html
23.66
KB
-rw-r--r--
getpass.html
10.65
KB
-rw-r--r--
gettext.html
78.76
KB
-rw-r--r--
gl.html
22.09
KB
-rw-r--r--
glob.html
13.26
KB
-rw-r--r--
grp.html
10.49
KB
-rw-r--r--
gzip.html
18.99
KB
-rw-r--r--
hashlib.html
18.2
KB
-rw-r--r--
heapq.html
31.61
KB
-rw-r--r--
hmac.html
10.46
KB
-rw-r--r--
hotshot.html
18.65
KB
-rw-r--r--
htmllib.html
25.32
KB
-rw-r--r--
htmlparser.html
39.11
KB
-rw-r--r--
httplib.html
62.95
KB
-rw-r--r--
i18n.html
9.52
KB
-rw-r--r--
ic.html
17.17
KB
-rw-r--r--
idle.html
20.9
KB
-rw-r--r--
imageop.html
14.76
KB
-rw-r--r--
imaplib.html
51.99
KB
-rw-r--r--
imgfile.html
11.71
KB
-rw-r--r--
imghdr.html
11.3
KB
-rw-r--r--
imp.html
34.34
KB
-rw-r--r--
importlib.html
8.26
KB
-rw-r--r--
imputil.html
31.81
KB
-rw-r--r--
index.html
72.78
KB
-rw-r--r--
inspect.html
50.71
KB
-rw-r--r--
internet.html
24.87
KB
-rw-r--r--
intro.html
8.93
KB
-rw-r--r--
io.html
98.13
KB
-rw-r--r--
ipc.html
13.41
KB
-rw-r--r--
itertools.html
115.91
KB
-rw-r--r--
jpeg.html
12.74
KB
-rw-r--r--
json.html
67.04
KB
-rw-r--r--
keyword.html
7.68
KB
-rw-r--r--
language.html
11.03
KB
-rw-r--r--
linecache.html
10.59
KB
-rw-r--r--
locale.html
55.14
KB
-rw-r--r--
logging.config.html
63.36
KB
-rw-r--r--
logging.handlers.html
69.64
KB
-rw-r--r--
logging.html
95.64
KB
-rw-r--r--
mac.html
21.79
KB
-rw-r--r--
macos.html
14.76
KB
-rw-r--r--
macosa.html
12.96
KB
-rw-r--r--
macostools.html
15.52
KB
-rw-r--r--
macpath.html
7.76
KB
-rw-r--r--
mailbox.html
156.75
KB
-rw-r--r--
mailcap.html
13.21
KB
-rw-r--r--
markup.html
18.77
KB
-rw-r--r--
marshal.html
17.98
KB
-rw-r--r--
math.html
39.24
KB
-rw-r--r--
md5.html
13.97
KB
-rw-r--r--
mhlib.html
21.54
KB
-rw-r--r--
mimetools.html
19.25
KB
-rw-r--r--
mimetypes.html
28.39
KB
-rw-r--r--
mimewriter.html
15.02
KB
-rw-r--r--
mimify.html
13.36
KB
-rw-r--r--
miniaeframe.html
12.2
KB
-rw-r--r--
misc.html
6.87
KB
-rw-r--r--
mm.html
9.03
KB
-rw-r--r--
mmap.html
28.36
KB
-rw-r--r--
modulefinder.html
15.31
KB
-rw-r--r--
modules.html
8.46
KB
-rw-r--r--
msilib.html
52.43
KB
-rw-r--r--
msvcrt.html
19.37
KB
-rw-r--r--
multifile.html
24.3
KB
-rw-r--r--
multiprocessing.html
365.71
KB
-rw-r--r--
mutex.html
11.23
KB
-rw-r--r--
netdata.html
16.98
KB
-rw-r--r--
netrc.html
12.3
KB
-rw-r--r--
new.html
12.12
KB
-rw-r--r--
nis.html
10.64
KB
-rw-r--r--
nntplib.html
41.92
KB
-rw-r--r--
numbers.html
37.75
KB
-rw-r--r--
numeric.html
13.55
KB
-rw-r--r--
operator.html
82
KB
-rw-r--r--
optparse.html
222.56
KB
-rw-r--r--
os.html
214.25
KB
-rw-r--r--
os.path.html
38.34
KB
-rw-r--r--
ossaudiodev.html
41.5
KB
-rw-r--r--
othergui.html
9.08
KB
-rw-r--r--
parser.html
39.36
KB
-rw-r--r--
pdb.html
33.96
KB
-rw-r--r--
persistence.html
14.87
KB
-rw-r--r--
pickle.html
102.27
KB
-rw-r--r--
pickletools.html
10.63
KB
-rw-r--r--
pipes.html
18.01
KB
-rw-r--r--
pkgutil.html
25.11
KB
-rw-r--r--
platform.html
28.37
KB
-rw-r--r--
plistlib.html
17.03
KB
-rw-r--r--
popen2.html
25.43
KB
-rw-r--r--
poplib.html
22.32
KB
-rw-r--r--
posix.html
14.41
KB
-rw-r--r--
posixfile.html
19.76
KB
-rw-r--r--
pprint.html
29.92
KB
-rw-r--r--
profile.html
63.56
KB
-rw-r--r--
pty.html
9.48
KB
-rw-r--r--
pwd.html
11.43
KB
-rw-r--r--
py_compile.html
11.12
KB
-rw-r--r--
pyclbr.html
14.71
KB
-rw-r--r--
pydoc.html
11.48
KB
-rw-r--r--
pyexpat.html
71.53
KB
-rw-r--r--
python.html
12.27
KB
-rw-r--r--
queue.html
24.22
KB
-rw-r--r--
quopri.html
11.9
KB
-rw-r--r--
random.html
37.83
KB
-rw-r--r--
re.html
134.74
KB
-rw-r--r--
readline.html
28.24
KB
-rw-r--r--
repr.html
20.43
KB
-rw-r--r--
resource.html
26.48
KB
-rw-r--r--
restricted.html
11.65
KB
-rw-r--r--
rexec.html
37.41
KB
-rw-r--r--
rfc822.html
42.22
KB
-rw-r--r--
rlcompleter.html
13.51
KB
-rw-r--r--
robotparser.html
12.27
KB
-rw-r--r--
runpy.html
19.34
KB
-rw-r--r--
sched.html
18.54
KB
-rw-r--r--
scrolledtext.html
9.32
KB
-rw-r--r--
select.html
39.67
KB
-rw-r--r--
sets.html
36.92
KB
-rw-r--r--
sgi.html
9.71
KB
-rw-r--r--
sgmllib.html
30.77
KB
-rw-r--r--
sha.html
12.09
KB
-rw-r--r--
shelve.html
27.02
KB
-rw-r--r--
shlex.html
32.1
KB
-rw-r--r--
shutil.html
40.22
KB
-rw-r--r--
signal.html
31.14
KB
-rw-r--r--
simplehttpserver.html
18.41
KB
-rw-r--r--
simplexmlrpcserver.html
31.39
KB
-rw-r--r--
site.html
23.64
KB
-rw-r--r--
smtpd.html
12.46
KB
-rw-r--r--
smtplib.html
42.13
KB
-rw-r--r--
sndhdr.html
10.02
KB
-rw-r--r--
socket.html
106.34
KB
-rw-r--r--
socketserver.html
59.83
KB
-rw-r--r--
someos.html
15.11
KB
-rw-r--r--
spwd.html
10.33
KB
-rw-r--r--
sqlite3.html
139.5
KB
-rw-r--r--
ssl.html
65.62
KB
-rw-r--r--
stat.html
32.31
KB
-rw-r--r--
statvfs.html
10.6
KB
-rw-r--r--
stdtypes.html
260.4
KB
-rw-r--r--
string.html
106.65
KB
-rw-r--r--
stringio.html
18.81
KB
-rw-r--r--
stringprep.html
16.13
KB
-rw-r--r--
strings.html
14.93
KB
-rw-r--r--
struct.html
40.88
KB
-rw-r--r--
subprocess.html
84.91
KB
-rw-r--r--
sun.html
6.84
KB
-rw-r--r--
sunau.html
27.1
KB
-rw-r--r--
sunaudio.html
17.79
KB
-rw-r--r--
symbol.html
7.66
KB
-rw-r--r--
symtable.html
22.94
KB
-rw-r--r--
sys.html
98.7
KB
-rw-r--r--
sysconfig.html
23.84
KB
-rw-r--r--
syslog.html
17.92
KB
-rw-r--r--
tabnanny.html
10.63
KB
-rw-r--r--
tarfile.html
78.68
KB
-rw-r--r--
telnetlib.html
25.48
KB
-rw-r--r--
tempfile.html
29.42
KB
-rw-r--r--
termios.html
16.01
KB
-rw-r--r--
test.html
52.62
KB
-rw-r--r--
textwrap.html
27.25
KB
-rw-r--r--
thread.html
20.47
KB
-rw-r--r--
threading.html
76.69
KB
-rw-r--r--
time.html
56.93
KB
-rw-r--r--
timeit.html
36.27
KB
-rw-r--r--
tix.html
46.96
KB
-rw-r--r--
tk.html
23.64
KB
-rw-r--r--
tkinter.html
67.67
KB
-rw-r--r--
token.html
19.62
KB
-rw-r--r--
tokenize.html
18.45
KB
-rw-r--r--
trace.html
25.54
KB
-rw-r--r--
traceback.html
33.44
KB
-rw-r--r--
ttk.html
101.75
KB
-rw-r--r--
tty.html
9.06
KB
-rw-r--r--
turtle.html
211.74
KB
-rw-r--r--
types.html
27.59
KB
-rw-r--r--
undoc.html
23.16
KB
-rw-r--r--
unicodedata.html
18.55
KB
-rw-r--r--
unittest.html
202.85
KB
-rw-r--r--
unix.html
10.55
KB
-rw-r--r--
urllib.html
58.68
KB
-rw-r--r--
urllib2.html
100.58
KB
-rw-r--r--
urlparse.html
40.41
KB
-rw-r--r--
user.html
11.83
KB
-rw-r--r--
userdict.html
29.73
KB
-rw-r--r--
uu.html
11.03
KB
-rw-r--r--
uuid.html
28.19
KB
-rw-r--r--
warnings.html
46.6
KB
-rw-r--r--
wave.html
22.22
KB
-rw-r--r--
weakref.html
36.52
KB
-rw-r--r--
webbrowser.html
23.07
KB
-rw-r--r--
whichdb.html
8.85
KB
-rw-r--r--
windows.html
9.33
KB
-rw-r--r--
winsound.html
18.75
KB
-rw-r--r--
wsgiref.html
81.04
KB
-rw-r--r--
xdrlib.html
29.94
KB
-rw-r--r--
xml.dom.html
89.04
KB
-rw-r--r--
xml.dom.minidom.html
40.42
KB
-rw-r--r--
xml.dom.pulldom.html
12.71
KB
-rw-r--r--
xml.etree.elementtree.html
93.22
KB
-rw-r--r--
xml.html
16.49
KB
-rw-r--r--
xml.sax.handler.html
38.63
KB
-rw-r--r--
xml.sax.html
20.22
KB
-rw-r--r--
xml.sax.reader.html
39.09
KB
-rw-r--r--
xml.sax.utils.html
14.26
KB
-rw-r--r--
xmlrpclib.html
60.79
KB
-rw-r--r--
zipfile.html
53.14
KB
-rw-r--r--
zipimport.html
20.42
KB
-rw-r--r--
zlib.html
25.46
KB
-rw-r--r--
Delete
Unzip
Zip
${this.title}
Close
Code Editor : itertools.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>9.7. itertools — Functions creating iterators for efficient looping — Python 2.7.5 documentation</title> <link rel="stylesheet" href="../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '2.7.5', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.5 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="top" title="Python 2.7.5 documentation" href="../index.html" /> <link rel="up" title="9. Numeric and Mathematical Modules" href="numeric.html" /> <link rel="next" title="9.8. functools — Higher-order functions and operations on callable objects" href="functools.html" /> <link rel="prev" title="9.6. random — Generate pseudo-random numbers" href="random.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="functools.html" title="9.8. functools — Higher-order functions and operations on callable objects" accesskey="N">next</a> |</li> <li class="right" > <a href="random.html" title="9.6. random — Generate pseudo-random numbers" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="numeric.html" accesskey="U">9. Numeric and Mathematical Modules</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="module-itertools"> <span id="itertools-functions-creating-iterators-for-efficient-looping"></span><h1>9.7. <a class="reference internal" href="#module-itertools" title="itertools: Functions creating iterators for efficient looping."><tt class="xref py py-mod docutils literal"><span class="pre">itertools</span></tt></a> — Functions creating iterators for efficient looping<a class="headerlink" href="#module-itertools" title="Permalink to this headline">¶</a></h1> <p class="versionadded"> <span class="versionmodified">New in version 2.3.</span></p> <p>This module implements a number of <a class="reference internal" href="../glossary.html#term-iterator"><em class="xref std std-term">iterator</em></a> building blocks inspired by constructs from APL, Haskell, and SML. Each has been recast in a form suitable for Python.</p> <p>The module standardizes a core set of fast, memory efficient tools that are useful by themselves or in combination. Together, they form an “iterator algebra” making it possible to construct specialized tools succinctly and efficiently in pure Python.</p> <p>For instance, SML provides a tabulation tool: <tt class="docutils literal"><span class="pre">tabulate(f)</span></tt> which produces a sequence <tt class="docutils literal"><span class="pre">f(0),</span> <span class="pre">f(1),</span> <span class="pre">...</span></tt>. The same effect can be achieved in Python by combining <a class="reference internal" href="#itertools.imap" title="itertools.imap"><tt class="xref py py-func docutils literal"><span class="pre">imap()</span></tt></a> and <a class="reference internal" href="#itertools.count" title="itertools.count"><tt class="xref py py-func docutils literal"><span class="pre">count()</span></tt></a> to form <tt class="docutils literal"><span class="pre">imap(f,</span> <span class="pre">count())</span></tt>.</p> <p>These tools and their built-in counterparts also work well with the high-speed functions in the <a class="reference internal" href="operator.html#module-operator" title="operator: Functions corresponding to the standard operators."><tt class="xref py py-mod docutils literal"><span class="pre">operator</span></tt></a> module. For example, the multiplication operator can be mapped across two vectors to form an efficient dot-product: <tt class="docutils literal"><span class="pre">sum(imap(operator.mul,</span> <span class="pre">vector1,</span> <span class="pre">vector2))</span></tt>.</p> <p><strong>Infinite Iterators:</strong></p> <table border="1" class="docutils"> <colgroup> <col width="14%" /> <col width="14%" /> <col width="39%" /> <col width="33%" /> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Iterator</th> <th class="head">Arguments</th> <th class="head">Results</th> <th class="head">Example</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td><a class="reference internal" href="#itertools.count" title="itertools.count"><tt class="xref py py-func docutils literal"><span class="pre">count()</span></tt></a></td> <td>start, [step]</td> <td>start, start+step, start+2*step, ...</td> <td><tt class="docutils literal"><span class="pre">count(10)</span> <span class="pre">--></span> <span class="pre">10</span> <span class="pre">11</span> <span class="pre">12</span> <span class="pre">13</span> <span class="pre">14</span> <span class="pre">...</span></tt></td> </tr> <tr class="row-odd"><td><a class="reference internal" href="#itertools.cycle" title="itertools.cycle"><tt class="xref py py-func docutils literal"><span class="pre">cycle()</span></tt></a></td> <td>p</td> <td>p0, p1, ... plast, p0, p1, ...</td> <td><tt class="docutils literal"><span class="pre">cycle('ABCD')</span> <span class="pre">--></span> <span class="pre">A</span> <span class="pre">B</span> <span class="pre">C</span> <span class="pre">D</span> <span class="pre">A</span> <span class="pre">B</span> <span class="pre">C</span> <span class="pre">D</span> <span class="pre">...</span></tt></td> </tr> <tr class="row-even"><td><a class="reference internal" href="#itertools.repeat" title="itertools.repeat"><tt class="xref py py-func docutils literal"><span class="pre">repeat()</span></tt></a></td> <td>elem [,n]</td> <td>elem, elem, elem, ... endlessly or up to n times</td> <td><tt class="docutils literal"><span class="pre">repeat(10,</span> <span class="pre">3)</span> <span class="pre">--></span> <span class="pre">10</span> <span class="pre">10</span> <span class="pre">10</span></tt></td> </tr> </tbody> </table> <p><strong>Iterators terminating on the shortest input sequence:</strong></p> <table border="1" class="docutils"> <colgroup> <col width="13%" /> <col width="18%" /> <col width="31%" /> <col width="39%" /> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Iterator</th> <th class="head">Arguments</th> <th class="head">Results</th> <th class="head">Example</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td><a class="reference internal" href="#itertools.chain" title="itertools.chain"><tt class="xref py py-func docutils literal"><span class="pre">chain()</span></tt></a></td> <td>p, q, ...</td> <td>p0, p1, ... plast, q0, q1, ...</td> <td><tt class="docutils literal"><span class="pre">chain('ABC',</span> <span class="pre">'DEF')</span> <span class="pre">--></span> <span class="pre">A</span> <span class="pre">B</span> <span class="pre">C</span> <span class="pre">D</span> <span class="pre">E</span> <span class="pre">F</span></tt></td> </tr> <tr class="row-odd"><td><a class="reference internal" href="#itertools.compress" title="itertools.compress"><tt class="xref py py-func docutils literal"><span class="pre">compress()</span></tt></a></td> <td>data, selectors</td> <td>(d[0] if s[0]), (d[1] if s[1]), ...</td> <td><tt class="docutils literal"><span class="pre">compress('ABCDEF',</span> <span class="pre">[1,0,1,0,1,1])</span> <span class="pre">--></span> <span class="pre">A</span> <span class="pre">C</span> <span class="pre">E</span> <span class="pre">F</span></tt></td> </tr> <tr class="row-even"><td><a class="reference internal" href="#itertools.dropwhile" title="itertools.dropwhile"><tt class="xref py py-func docutils literal"><span class="pre">dropwhile()</span></tt></a></td> <td>pred, seq</td> <td>seq[n], seq[n+1], starting when pred fails</td> <td><tt class="docutils literal"><span class="pre">dropwhile(lambda</span> <span class="pre">x:</span> <span class="pre">x<5,</span> <span class="pre">[1,4,6,4,1])</span> <span class="pre">--></span> <span class="pre">6</span> <span class="pre">4</span> <span class="pre">1</span></tt></td> </tr> <tr class="row-odd"><td><a class="reference internal" href="#itertools.groupby" title="itertools.groupby"><tt class="xref py py-func docutils literal"><span class="pre">groupby()</span></tt></a></td> <td>iterable[, keyfunc]</td> <td>sub-iterators grouped by value of keyfunc(v)</td> <td> </td> </tr> <tr class="row-even"><td><a class="reference internal" href="#itertools.ifilter" title="itertools.ifilter"><tt class="xref py py-func docutils literal"><span class="pre">ifilter()</span></tt></a></td> <td>pred, seq</td> <td>elements of seq where pred(elem) is True</td> <td><tt class="docutils literal"><span class="pre">ifilter(lambda</span> <span class="pre">x:</span> <span class="pre">x%2,</span> <span class="pre">range(10))</span> <span class="pre">--></span> <span class="pre">1</span> <span class="pre">3</span> <span class="pre">5</span> <span class="pre">7</span> <span class="pre">9</span></tt></td> </tr> <tr class="row-odd"><td><a class="reference internal" href="#itertools.ifilterfalse" title="itertools.ifilterfalse"><tt class="xref py py-func docutils literal"><span class="pre">ifilterfalse()</span></tt></a></td> <td>pred, seq</td> <td>elements of seq where pred(elem) is False</td> <td><tt class="docutils literal"><span class="pre">ifilterfalse(lambda</span> <span class="pre">x:</span> <span class="pre">x%2,</span> <span class="pre">range(10))</span> <span class="pre">--></span> <span class="pre">0</span> <span class="pre">2</span> <span class="pre">4</span> <span class="pre">6</span> <span class="pre">8</span></tt></td> </tr> <tr class="row-even"><td><a class="reference internal" href="#itertools.islice" title="itertools.islice"><tt class="xref py py-func docutils literal"><span class="pre">islice()</span></tt></a></td> <td>seq, [start,] stop [, step]</td> <td>elements from seq[start:stop:step]</td> <td><tt class="docutils literal"><span class="pre">islice('ABCDEFG',</span> <span class="pre">2,</span> <span class="pre">None)</span> <span class="pre">--></span> <span class="pre">C</span> <span class="pre">D</span> <span class="pre">E</span> <span class="pre">F</span> <span class="pre">G</span></tt></td> </tr> <tr class="row-odd"><td><a class="reference internal" href="#itertools.imap" title="itertools.imap"><tt class="xref py py-func docutils literal"><span class="pre">imap()</span></tt></a></td> <td>func, p, q, ...</td> <td>func(p0, q0), func(p1, q1), ...</td> <td><tt class="docutils literal"><span class="pre">imap(pow,</span> <span class="pre">(2,3,10),</span> <span class="pre">(5,2,3))</span> <span class="pre">--></span> <span class="pre">32</span> <span class="pre">9</span> <span class="pre">1000</span></tt></td> </tr> <tr class="row-even"><td><a class="reference internal" href="#itertools.starmap" title="itertools.starmap"><tt class="xref py py-func docutils literal"><span class="pre">starmap()</span></tt></a></td> <td>func, seq</td> <td>func(*seq[0]), func(*seq[1]), ...</td> <td><tt class="docutils literal"><span class="pre">starmap(pow,</span> <span class="pre">[(2,5),</span> <span class="pre">(3,2),</span> <span class="pre">(10,3)])</span> <span class="pre">--></span> <span class="pre">32</span> <span class="pre">9</span> <span class="pre">1000</span></tt></td> </tr> <tr class="row-odd"><td><a class="reference internal" href="#itertools.tee" title="itertools.tee"><tt class="xref py py-func docutils literal"><span class="pre">tee()</span></tt></a></td> <td>it, n</td> <td>it1, it2 , ... itn splits one iterator into n</td> <td> </td> </tr> <tr class="row-even"><td><a class="reference internal" href="#itertools.takewhile" title="itertools.takewhile"><tt class="xref py py-func docutils literal"><span class="pre">takewhile()</span></tt></a></td> <td>pred, seq</td> <td>seq[0], seq[1], until pred fails</td> <td><tt class="docutils literal"><span class="pre">takewhile(lambda</span> <span class="pre">x:</span> <span class="pre">x<5,</span> <span class="pre">[1,4,6,4,1])</span> <span class="pre">--></span> <span class="pre">1</span> <span class="pre">4</span></tt></td> </tr> <tr class="row-odd"><td><a class="reference internal" href="#itertools.izip" title="itertools.izip"><tt class="xref py py-func docutils literal"><span class="pre">izip()</span></tt></a></td> <td>p, q, ...</td> <td>(p[0], q[0]), (p[1], q[1]), ...</td> <td><tt class="docutils literal"><span class="pre">izip('ABCD',</span> <span class="pre">'xy')</span> <span class="pre">--></span> <span class="pre">Ax</span> <span class="pre">By</span></tt></td> </tr> <tr class="row-even"><td><a class="reference internal" href="#itertools.izip_longest" title="itertools.izip_longest"><tt class="xref py py-func docutils literal"><span class="pre">izip_longest()</span></tt></a></td> <td>p, q, ...</td> <td>(p[0], q[0]), (p[1], q[1]), ...</td> <td><tt class="docutils literal"><span class="pre">izip_longest('ABCD',</span> <span class="pre">'xy',</span> <span class="pre">fillvalue='-')</span> <span class="pre">--></span> <span class="pre">Ax</span> <span class="pre">By</span> <span class="pre">C-</span> <span class="pre">D-</span></tt></td> </tr> </tbody> </table> <p><strong>Combinatoric generators:</strong></p> <table border="1" class="docutils"> <colgroup> <col width="36%" /> <col width="16%" /> <col width="48%" /> </colgroup> <thead valign="bottom"> <tr class="row-odd"><th class="head">Iterator</th> <th class="head">Arguments</th> <th class="head">Results</th> </tr> </thead> <tbody valign="top"> <tr class="row-even"><td><a class="reference internal" href="#itertools.product" title="itertools.product"><tt class="xref py py-func docutils literal"><span class="pre">product()</span></tt></a></td> <td>p, q, ... [repeat=1]</td> <td>cartesian product, equivalent to a nested for-loop</td> </tr> <tr class="row-odd"><td><a class="reference internal" href="#itertools.permutations" title="itertools.permutations"><tt class="xref py py-func docutils literal"><span class="pre">permutations()</span></tt></a></td> <td>p[, r]</td> <td>r-length tuples, all possible orderings, no repeated elements</td> </tr> <tr class="row-even"><td><a class="reference internal" href="#itertools.combinations" title="itertools.combinations"><tt class="xref py py-func docutils literal"><span class="pre">combinations()</span></tt></a></td> <td>p, r</td> <td>r-length tuples, in sorted order, no repeated elements</td> </tr> <tr class="row-odd"><td><a class="reference internal" href="#itertools.combinations_with_replacement" title="itertools.combinations_with_replacement"><tt class="xref py py-func docutils literal"><span class="pre">combinations_with_replacement()</span></tt></a></td> <td>p, r</td> <td>r-length tuples, in sorted order, with repeated elements</td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">product('ABCD',</span> <span class="pre">repeat=2)</span></tt></td> <td> </td> <td><tt class="docutils literal"><span class="pre">AA</span> <span class="pre">AB</span> <span class="pre">AC</span> <span class="pre">AD</span> <span class="pre">BA</span> <span class="pre">BB</span> <span class="pre">BC</span> <span class="pre">BD</span> <span class="pre">CA</span> <span class="pre">CB</span> <span class="pre">CC</span> <span class="pre">CD</span> <span class="pre">DA</span> <span class="pre">DB</span> <span class="pre">DC</span> <span class="pre">DD</span></tt></td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">permutations('ABCD',</span> <span class="pre">2)</span></tt></td> <td> </td> <td><tt class="docutils literal"><span class="pre">AB</span> <span class="pre">AC</span> <span class="pre">AD</span> <span class="pre">BA</span> <span class="pre">BC</span> <span class="pre">BD</span> <span class="pre">CA</span> <span class="pre">CB</span> <span class="pre">CD</span> <span class="pre">DA</span> <span class="pre">DB</span> <span class="pre">DC</span></tt></td> </tr> <tr class="row-even"><td><tt class="docutils literal"><span class="pre">combinations('ABCD',</span> <span class="pre">2)</span></tt></td> <td> </td> <td><tt class="docutils literal"><span class="pre">AB</span> <span class="pre">AC</span> <span class="pre">AD</span> <span class="pre">BC</span> <span class="pre">BD</span> <span class="pre">CD</span></tt></td> </tr> <tr class="row-odd"><td><tt class="docutils literal"><span class="pre">combinations_with_replacement('ABCD',</span> <span class="pre">2)</span></tt></td> <td> </td> <td><tt class="docutils literal"><span class="pre">AA</span> <span class="pre">AB</span> <span class="pre">AC</span> <span class="pre">AD</span> <span class="pre">BB</span> <span class="pre">BC</span> <span class="pre">BD</span> <span class="pre">CC</span> <span class="pre">CD</span> <span class="pre">DD</span></tt></td> </tr> </tbody> </table> <div class="section" id="itertool-functions"> <span id="itertools-functions"></span><h2>9.7.1. Itertool functions<a class="headerlink" href="#itertool-functions" title="Permalink to this headline">¶</a></h2> <p>The following module functions all construct and return iterators. Some provide streams of infinite length, so they should only be accessed by functions or loops that truncate the stream.</p> <dl class="function"> <dt id="itertools.chain"> <tt class="descclassname">itertools.</tt><tt class="descname">chain</tt><big>(</big><em>*iterables</em><big>)</big><a class="headerlink" href="#itertools.chain" title="Permalink to this definition">¶</a></dt> <dd><p>Make an iterator that returns elements from the first iterable until it is exhausted, then proceeds to the next iterable, until all of the iterables are exhausted. Used for treating consecutive sequences as a single sequence. Equivalent to:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">chain</span><span class="p">(</span><span class="o">*</span><span class="n">iterables</span><span class="p">):</span> <span class="c"># chain('ABC', 'DEF') --> A B C D E F</span> <span class="k">for</span> <span class="n">it</span> <span class="ow">in</span> <span class="n">iterables</span><span class="p">:</span> <span class="k">for</span> <span class="n">element</span> <span class="ow">in</span> <span class="n">it</span><span class="p">:</span> <span class="k">yield</span> <span class="n">element</span> </pre></div> </div> </dd></dl> <dl class="classmethod"> <dt id="itertools.chain.from_iterable"> <em class="property">classmethod </em><tt class="descclassname">chain.</tt><tt class="descname">from_iterable</tt><big>(</big><em>iterable</em><big>)</big><a class="headerlink" href="#itertools.chain.from_iterable" title="Permalink to this definition">¶</a></dt> <dd><p>Alternate constructor for <a class="reference internal" href="#itertools.chain" title="itertools.chain"><tt class="xref py py-func docutils literal"><span class="pre">chain()</span></tt></a>. Gets chained inputs from a single iterable argument that is evaluated lazily. Roughly equivalent to:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">from_iterable</span><span class="p">(</span><span class="n">iterables</span><span class="p">):</span> <span class="c"># chain.from_iterable(['ABC', 'DEF']) --> A B C D E F</span> <span class="k">for</span> <span class="n">it</span> <span class="ow">in</span> <span class="n">iterables</span><span class="p">:</span> <span class="k">for</span> <span class="n">element</span> <span class="ow">in</span> <span class="n">it</span><span class="p">:</span> <span class="k">yield</span> <span class="n">element</span> </pre></div> </div> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="function"> <dt id="itertools.combinations"> <tt class="descclassname">itertools.</tt><tt class="descname">combinations</tt><big>(</big><em>iterable</em>, <em>r</em><big>)</big><a class="headerlink" href="#itertools.combinations" title="Permalink to this definition">¶</a></dt> <dd><p>Return <em>r</em> length subsequences of elements from the input <em>iterable</em>.</p> <p>Combinations are emitted in lexicographic sort order. So, if the input <em>iterable</em> is sorted, the combination tuples will be produced in sorted order.</p> <p>Elements are treated as unique based on their position, not on their value. So if the input elements are unique, there will be no repeat values in each combination.</p> <p>Equivalent to:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">combinations</span><span class="p">(</span><span class="n">iterable</span><span class="p">,</span> <span class="n">r</span><span class="p">):</span> <span class="c"># combinations('ABCD', 2) --> AB AC AD BC BD CD</span> <span class="c"># combinations(range(4), 3) --> 012 013 023 123</span> <span class="n">pool</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">iterable</span><span class="p">)</span> <span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">pool</span><span class="p">)</span> <span class="k">if</span> <span class="n">r</span> <span class="o">></span> <span class="n">n</span><span class="p">:</span> <span class="k">return</span> <span class="n">indices</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="n">r</span><span class="p">)</span> <span class="k">yield</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">pool</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">indices</span><span class="p">)</span> <span class="k">while</span> <span class="bp">True</span><span class="p">:</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="n">r</span><span class="p">)):</span> <span class="k">if</span> <span class="n">indices</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">!=</span> <span class="n">i</span> <span class="o">+</span> <span class="n">n</span> <span class="o">-</span> <span class="n">r</span><span class="p">:</span> <span class="k">break</span> <span class="k">else</span><span class="p">:</span> <span class="k">return</span> <span class="n">indices</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span> <span class="k">for</span> <span class="n">j</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">,</span> <span class="n">r</span><span class="p">):</span> <span class="n">indices</span><span class="p">[</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">indices</span><span class="p">[</span><span class="n">j</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span> <span class="k">yield</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">pool</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">indices</span><span class="p">)</span> </pre></div> </div> <p>The code for <a class="reference internal" href="#itertools.combinations" title="itertools.combinations"><tt class="xref py py-func docutils literal"><span class="pre">combinations()</span></tt></a> can be also expressed as a subsequence of <a class="reference internal" href="#itertools.permutations" title="itertools.permutations"><tt class="xref py py-func docutils literal"><span class="pre">permutations()</span></tt></a> after filtering entries where the elements are not in sorted order (according to their position in the input pool):</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">combinations</span><span class="p">(</span><span class="n">iterable</span><span class="p">,</span> <span class="n">r</span><span class="p">):</span> <span class="n">pool</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">iterable</span><span class="p">)</span> <span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">pool</span><span class="p">)</span> <span class="k">for</span> <span class="n">indices</span> <span class="ow">in</span> <span class="n">permutations</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">),</span> <span class="n">r</span><span class="p">):</span> <span class="k">if</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">indices</span><span class="p">)</span> <span class="o">==</span> <span class="nb">list</span><span class="p">(</span><span class="n">indices</span><span class="p">):</span> <span class="k">yield</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">pool</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">indices</span><span class="p">)</span> </pre></div> </div> <p>The number of items returned is <tt class="docutils literal"><span class="pre">n!</span> <span class="pre">/</span> <span class="pre">r!</span> <span class="pre">/</span> <span class="pre">(n-r)!</span></tt> when <tt class="docutils literal"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">r</span> <span class="pre"><=</span> <span class="pre">n</span></tt> or zero when <tt class="docutils literal"><span class="pre">r</span> <span class="pre">></span> <span class="pre">n</span></tt>.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="function"> <dt id="itertools.combinations_with_replacement"> <tt class="descclassname">itertools.</tt><tt class="descname">combinations_with_replacement</tt><big>(</big><em>iterable</em>, <em>r</em><big>)</big><a class="headerlink" href="#itertools.combinations_with_replacement" title="Permalink to this definition">¶</a></dt> <dd><p>Return <em>r</em> length subsequences of elements from the input <em>iterable</em> allowing individual elements to be repeated more than once.</p> <p>Combinations are emitted in lexicographic sort order. So, if the input <em>iterable</em> is sorted, the combination tuples will be produced in sorted order.</p> <p>Elements are treated as unique based on their position, not on their value. So if the input elements are unique, the generated combinations will also be unique.</p> <p>Equivalent to:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">combinations_with_replacement</span><span class="p">(</span><span class="n">iterable</span><span class="p">,</span> <span class="n">r</span><span class="p">):</span> <span class="c"># combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC</span> <span class="n">pool</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">iterable</span><span class="p">)</span> <span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">pool</span><span class="p">)</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">n</span> <span class="ow">and</span> <span class="n">r</span><span class="p">:</span> <span class="k">return</span> <span class="n">indices</span> <span class="o">=</span> <span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">*</span> <span class="n">r</span> <span class="k">yield</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">pool</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">indices</span><span class="p">)</span> <span class="k">while</span> <span class="bp">True</span><span class="p">:</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="n">r</span><span class="p">)):</span> <span class="k">if</span> <span class="n">indices</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">!=</span> <span class="n">n</span> <span class="o">-</span> <span class="mi">1</span><span class="p">:</span> <span class="k">break</span> <span class="k">else</span><span class="p">:</span> <span class="k">return</span> <span class="n">indices</span><span class="p">[</span><span class="n">i</span><span class="p">:]</span> <span class="o">=</span> <span class="p">[</span><span class="n">indices</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">+</span> <span class="mi">1</span><span class="p">]</span> <span class="o">*</span> <span class="p">(</span><span class="n">r</span> <span class="o">-</span> <span class="n">i</span><span class="p">)</span> <span class="k">yield</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">pool</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">indices</span><span class="p">)</span> </pre></div> </div> <p>The code for <a class="reference internal" href="#itertools.combinations_with_replacement" title="itertools.combinations_with_replacement"><tt class="xref py py-func docutils literal"><span class="pre">combinations_with_replacement()</span></tt></a> can be also expressed as a subsequence of <a class="reference internal" href="#itertools.product" title="itertools.product"><tt class="xref py py-func docutils literal"><span class="pre">product()</span></tt></a> after filtering entries where the elements are not in sorted order (according to their position in the input pool):</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">combinations_with_replacement</span><span class="p">(</span><span class="n">iterable</span><span class="p">,</span> <span class="n">r</span><span class="p">):</span> <span class="n">pool</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">iterable</span><span class="p">)</span> <span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">pool</span><span class="p">)</span> <span class="k">for</span> <span class="n">indices</span> <span class="ow">in</span> <span class="n">product</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">),</span> <span class="n">repeat</span><span class="o">=</span><span class="n">r</span><span class="p">):</span> <span class="k">if</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">indices</span><span class="p">)</span> <span class="o">==</span> <span class="nb">list</span><span class="p">(</span><span class="n">indices</span><span class="p">):</span> <span class="k">yield</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">pool</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">indices</span><span class="p">)</span> </pre></div> </div> <p>The number of items returned is <tt class="docutils literal"><span class="pre">(n+r-1)!</span> <span class="pre">/</span> <span class="pre">r!</span> <span class="pre">/</span> <span class="pre">(n-1)!</span></tt> when <tt class="docutils literal"><span class="pre">n</span> <span class="pre">></span> <span class="pre">0</span></tt>.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.7.</span></p> </dd></dl> <dl class="function"> <dt id="itertools.compress"> <tt class="descclassname">itertools.</tt><tt class="descname">compress</tt><big>(</big><em>data</em>, <em>selectors</em><big>)</big><a class="headerlink" href="#itertools.compress" title="Permalink to this definition">¶</a></dt> <dd><p>Make an iterator that filters elements from <em>data</em> returning only those that have a corresponding element in <em>selectors</em> that evaluates to <tt class="docutils literal"><span class="pre">True</span></tt>. Stops when either the <em>data</em> or <em>selectors</em> iterables has been exhausted. Equivalent to:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">compress</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">selectors</span><span class="p">):</span> <span class="c"># compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F</span> <span class="k">return</span> <span class="p">(</span><span class="n">d</span> <span class="k">for</span> <span class="n">d</span><span class="p">,</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">izip</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">selectors</span><span class="p">)</span> <span class="k">if</span> <span class="n">s</span><span class="p">)</span> </pre></div> </div> <p class="versionadded"> <span class="versionmodified">New in version 2.7.</span></p> </dd></dl> <dl class="function"> <dt id="itertools.count"> <tt class="descclassname">itertools.</tt><tt class="descname">count</tt><big>(</big><em>start=0</em>, <em>step=1</em><big>)</big><a class="headerlink" href="#itertools.count" title="Permalink to this definition">¶</a></dt> <dd><p>Make an iterator that returns evenly spaced values starting with <em>n</em>. Often used as an argument to <a class="reference internal" href="#itertools.imap" title="itertools.imap"><tt class="xref py py-func docutils literal"><span class="pre">imap()</span></tt></a> to generate consecutive data points. Also, used with <a class="reference internal" href="#itertools.izip" title="itertools.izip"><tt class="xref py py-func docutils literal"><span class="pre">izip()</span></tt></a> to add sequence numbers. Equivalent to:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">count</span><span class="p">(</span><span class="n">start</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">step</span><span class="o">=</span><span class="mi">1</span><span class="p">):</span> <span class="c"># count(10) --> 10 11 12 13 14 ...</span> <span class="c"># count(2.5, 0.5) -> 2.5 3.0 3.5 ...</span> <span class="n">n</span> <span class="o">=</span> <span class="n">start</span> <span class="k">while</span> <span class="bp">True</span><span class="p">:</span> <span class="k">yield</span> <span class="n">n</span> <span class="n">n</span> <span class="o">+=</span> <span class="n">step</span> </pre></div> </div> <p>When counting with floating point numbers, better accuracy can sometimes be achieved by substituting multiplicative code such as: <tt class="docutils literal"><span class="pre">(start</span> <span class="pre">+</span> <span class="pre">step</span> <span class="pre">*</span> <span class="pre">i</span> <span class="pre">for</span> <span class="pre">i</span> <span class="pre">in</span> <span class="pre">count())</span></tt>.</p> <p class="versionchanged"> <span class="versionmodified">Changed in version 2.7: </span>added <em>step</em> argument and allowed non-integer arguments.</p> </dd></dl> <dl class="function"> <dt id="itertools.cycle"> <tt class="descclassname">itertools.</tt><tt class="descname">cycle</tt><big>(</big><em>iterable</em><big>)</big><a class="headerlink" href="#itertools.cycle" title="Permalink to this definition">¶</a></dt> <dd><p>Make an iterator returning elements from the iterable and saving a copy of each. When the iterable is exhausted, return elements from the saved copy. Repeats indefinitely. Equivalent to:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">cycle</span><span class="p">(</span><span class="n">iterable</span><span class="p">):</span> <span class="c"># cycle('ABCD') --> A B C D A B C D A B C D ...</span> <span class="n">saved</span> <span class="o">=</span> <span class="p">[]</span> <span class="k">for</span> <span class="n">element</span> <span class="ow">in</span> <span class="n">iterable</span><span class="p">:</span> <span class="k">yield</span> <span class="n">element</span> <span class="n">saved</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">element</span><span class="p">)</span> <span class="k">while</span> <span class="n">saved</span><span class="p">:</span> <span class="k">for</span> <span class="n">element</span> <span class="ow">in</span> <span class="n">saved</span><span class="p">:</span> <span class="k">yield</span> <span class="n">element</span> </pre></div> </div> <p>Note, this member of the toolkit may require significant auxiliary storage (depending on the length of the iterable).</p> </dd></dl> <dl class="function"> <dt id="itertools.dropwhile"> <tt class="descclassname">itertools.</tt><tt class="descname">dropwhile</tt><big>(</big><em>predicate</em>, <em>iterable</em><big>)</big><a class="headerlink" href="#itertools.dropwhile" title="Permalink to this definition">¶</a></dt> <dd><p>Make an iterator that drops elements from the iterable as long as the predicate is true; afterwards, returns every element. Note, the iterator does not produce <em>any</em> output until the predicate first becomes false, so it may have a lengthy start-up time. Equivalent to:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">dropwhile</span><span class="p">(</span><span class="n">predicate</span><span class="p">,</span> <span class="n">iterable</span><span class="p">):</span> <span class="c"># dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1</span> <span class="n">iterable</span> <span class="o">=</span> <span class="nb">iter</span><span class="p">(</span><span class="n">iterable</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">iterable</span><span class="p">:</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">predicate</span><span class="p">(</span><span class="n">x</span><span class="p">):</span> <span class="k">yield</span> <span class="n">x</span> <span class="k">break</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">iterable</span><span class="p">:</span> <span class="k">yield</span> <span class="n">x</span> </pre></div> </div> </dd></dl> <dl class="function"> <dt id="itertools.groupby"> <tt class="descclassname">itertools.</tt><tt class="descname">groupby</tt><big>(</big><em>iterable</em><span class="optional">[</span>, <em>key</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#itertools.groupby" title="Permalink to this definition">¶</a></dt> <dd><p>Make an iterator that returns consecutive keys and groups from the <em>iterable</em>. The <em>key</em> is a function computing a key value for each element. If not specified or is <tt class="docutils literal"><span class="pre">None</span></tt>, <em>key</em> defaults to an identity function and returns the element unchanged. Generally, the iterable needs to already be sorted on the same key function.</p> <p>The operation of <a class="reference internal" href="#itertools.groupby" title="itertools.groupby"><tt class="xref py py-func docutils literal"><span class="pre">groupby()</span></tt></a> is similar to the <tt class="docutils literal"><span class="pre">uniq</span></tt> filter in Unix. It generates a break or new group every time the value of the key function changes (which is why it is usually necessary to have sorted the data using the same key function). That behavior differs from SQL’s GROUP BY which aggregates common elements regardless of their input order.</p> <p>The returned group is itself an iterator that shares the underlying iterable with <a class="reference internal" href="#itertools.groupby" title="itertools.groupby"><tt class="xref py py-func docutils literal"><span class="pre">groupby()</span></tt></a>. Because the source is shared, when the <a class="reference internal" href="#itertools.groupby" title="itertools.groupby"><tt class="xref py py-func docutils literal"><span class="pre">groupby()</span></tt></a> object is advanced, the previous group is no longer visible. So, if that data is needed later, it should be stored as a list:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">groups</span> <span class="o">=</span> <span class="p">[]</span> <span class="n">uniquekeys</span> <span class="o">=</span> <span class="p">[]</span> <span class="n">data</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="n">keyfunc</span><span class="p">)</span> <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">g</span> <span class="ow">in</span> <span class="n">groupby</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">keyfunc</span><span class="p">):</span> <span class="n">groups</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">g</span><span class="p">))</span> <span class="c"># Store group iterator as a list</span> <span class="n">uniquekeys</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">k</span><span class="p">)</span> </pre></div> </div> <p><a class="reference internal" href="#itertools.groupby" title="itertools.groupby"><tt class="xref py py-func docutils literal"><span class="pre">groupby()</span></tt></a> is equivalent to:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">groupby</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span> <span class="c"># [k for k, g in groupby('AAAABBBCCDAABBB')] --> A B C D A B</span> <span class="c"># [list(g) for k, g in groupby('AAAABBBCCD')] --> AAAA BBB CC D</span> <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">iterable</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span> <span class="k">if</span> <span class="n">key</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="n">key</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span> <span class="bp">self</span><span class="o">.</span><span class="n">keyfunc</span> <span class="o">=</span> <span class="n">key</span> <span class="bp">self</span><span class="o">.</span><span class="n">it</span> <span class="o">=</span> <span class="nb">iter</span><span class="p">(</span><span class="n">iterable</span><span class="p">)</span> <span class="bp">self</span><span class="o">.</span><span class="n">tgtkey</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">currkey</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">currvalue</span> <span class="o">=</span> <span class="nb">object</span><span class="p">()</span> <span class="k">def</span> <span class="nf">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">return</span> <span class="bp">self</span> <span class="k">def</span> <span class="nf">next</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">while</span> <span class="bp">self</span><span class="o">.</span><span class="n">currkey</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">tgtkey</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">currvalue</span> <span class="o">=</span> <span class="nb">next</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">it</span><span class="p">)</span> <span class="c"># Exit on StopIteration</span> <span class="bp">self</span><span class="o">.</span><span class="n">currkey</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">keyfunc</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">currvalue</span><span class="p">)</span> <span class="bp">self</span><span class="o">.</span><span class="n">tgtkey</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">currkey</span> <span class="k">return</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">currkey</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_grouper</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">tgtkey</span><span class="p">))</span> <span class="k">def</span> <span class="nf">_grouper</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tgtkey</span><span class="p">):</span> <span class="k">while</span> <span class="bp">self</span><span class="o">.</span><span class="n">currkey</span> <span class="o">==</span> <span class="n">tgtkey</span><span class="p">:</span> <span class="k">yield</span> <span class="bp">self</span><span class="o">.</span><span class="n">currvalue</span> <span class="bp">self</span><span class="o">.</span><span class="n">currvalue</span> <span class="o">=</span> <span class="nb">next</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">it</span><span class="p">)</span> <span class="c"># Exit on StopIteration</span> <span class="bp">self</span><span class="o">.</span><span class="n">currkey</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">keyfunc</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">currvalue</span><span class="p">)</span> </pre></div> </div> <p class="versionadded"> <span class="versionmodified">New in version 2.4.</span></p> </dd></dl> <dl class="function"> <dt id="itertools.ifilter"> <tt class="descclassname">itertools.</tt><tt class="descname">ifilter</tt><big>(</big><em>predicate</em>, <em>iterable</em><big>)</big><a class="headerlink" href="#itertools.ifilter" title="Permalink to this definition">¶</a></dt> <dd><p>Make an iterator that filters elements from iterable returning only those for which the predicate is <tt class="docutils literal"><span class="pre">True</span></tt>. If <em>predicate</em> is <tt class="docutils literal"><span class="pre">None</span></tt>, return the items that are true. Equivalent to:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">ifilter</span><span class="p">(</span><span class="n">predicate</span><span class="p">,</span> <span class="n">iterable</span><span class="p">):</span> <span class="c"># ifilter(lambda x: x%2, range(10)) --> 1 3 5 7 9</span> <span class="k">if</span> <span class="n">predicate</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="n">predicate</span> <span class="o">=</span> <span class="nb">bool</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">iterable</span><span class="p">:</span> <span class="k">if</span> <span class="n">predicate</span><span class="p">(</span><span class="n">x</span><span class="p">):</span> <span class="k">yield</span> <span class="n">x</span> </pre></div> </div> </dd></dl> <dl class="function"> <dt id="itertools.ifilterfalse"> <tt class="descclassname">itertools.</tt><tt class="descname">ifilterfalse</tt><big>(</big><em>predicate</em>, <em>iterable</em><big>)</big><a class="headerlink" href="#itertools.ifilterfalse" title="Permalink to this definition">¶</a></dt> <dd><p>Make an iterator that filters elements from iterable returning only those for which the predicate is <tt class="docutils literal"><span class="pre">False</span></tt>. If <em>predicate</em> is <tt class="docutils literal"><span class="pre">None</span></tt>, return the items that are false. Equivalent to:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">ifilterfalse</span><span class="p">(</span><span class="n">predicate</span><span class="p">,</span> <span class="n">iterable</span><span class="p">):</span> <span class="c"># ifilterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8</span> <span class="k">if</span> <span class="n">predicate</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="n">predicate</span> <span class="o">=</span> <span class="nb">bool</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">iterable</span><span class="p">:</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">predicate</span><span class="p">(</span><span class="n">x</span><span class="p">):</span> <span class="k">yield</span> <span class="n">x</span> </pre></div> </div> </dd></dl> <dl class="function"> <dt id="itertools.imap"> <tt class="descclassname">itertools.</tt><tt class="descname">imap</tt><big>(</big><em>function</em>, <em>*iterables</em><big>)</big><a class="headerlink" href="#itertools.imap" title="Permalink to this definition">¶</a></dt> <dd><p>Make an iterator that computes the function using arguments from each of the iterables. If <em>function</em> is set to <tt class="docutils literal"><span class="pre">None</span></tt>, then <a class="reference internal" href="#itertools.imap" title="itertools.imap"><tt class="xref py py-func docutils literal"><span class="pre">imap()</span></tt></a> returns the arguments as a tuple. Like <a class="reference internal" href="functions.html#map" title="map"><tt class="xref py py-func docutils literal"><span class="pre">map()</span></tt></a> but stops when the shortest iterable is exhausted instead of filling in <tt class="docutils literal"><span class="pre">None</span></tt> for shorter iterables. The reason for the difference is that infinite iterator arguments are typically an error for <a class="reference internal" href="functions.html#map" title="map"><tt class="xref py py-func docutils literal"><span class="pre">map()</span></tt></a> (because the output is fully evaluated) but represent a common and useful way of supplying arguments to <a class="reference internal" href="#itertools.imap" title="itertools.imap"><tt class="xref py py-func docutils literal"><span class="pre">imap()</span></tt></a>. Equivalent to:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">imap</span><span class="p">(</span><span class="n">function</span><span class="p">,</span> <span class="o">*</span><span class="n">iterables</span><span class="p">):</span> <span class="c"># imap(pow, (2,3,10), (5,2,3)) --> 32 9 1000</span> <span class="n">iterables</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="nb">iter</span><span class="p">,</span> <span class="n">iterables</span><span class="p">)</span> <span class="k">while</span> <span class="bp">True</span><span class="p">:</span> <span class="n">args</span> <span class="o">=</span> <span class="p">[</span><span class="nb">next</span><span class="p">(</span><span class="n">it</span><span class="p">)</span> <span class="k">for</span> <span class="n">it</span> <span class="ow">in</span> <span class="n">iterables</span><span class="p">]</span> <span class="k">if</span> <span class="n">function</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="k">yield</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">args</span><span class="p">)</span> <span class="k">else</span><span class="p">:</span> <span class="k">yield</span> <span class="n">function</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">)</span> </pre></div> </div> </dd></dl> <dl class="function"> <dt id="itertools.islice"> <tt class="descclassname">itertools.</tt><tt class="descname">islice</tt><big>(</big><em>iterable</em>, <em>stop</em><big>)</big><a class="headerlink" href="#itertools.islice" title="Permalink to this definition">¶</a></dt> <dt> <tt class="descclassname">itertools.</tt><tt class="descname">islice</tt><big>(</big><em>iterable</em>, <em>start</em>, <em>stop</em><span class="optional">[</span>, <em>step</em><span class="optional">]</span><big>)</big></dt> <dd><p>Make an iterator that returns selected elements from the iterable. If <em>start</em> is non-zero, then elements from the iterable are skipped until start is reached. Afterward, elements are returned consecutively unless <em>step</em> is set higher than one which results in items being skipped. If <em>stop</em> is <tt class="docutils literal"><span class="pre">None</span></tt>, then iteration continues until the iterator is exhausted, if at all; otherwise, it stops at the specified position. Unlike regular slicing, <a class="reference internal" href="#itertools.islice" title="itertools.islice"><tt class="xref py py-func docutils literal"><span class="pre">islice()</span></tt></a> does not support negative values for <em>start</em>, <em>stop</em>, or <em>step</em>. Can be used to extract related fields from data where the internal structure has been flattened (for example, a multi-line report may list a name field on every third line). Equivalent to:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">islice</span><span class="p">(</span><span class="n">iterable</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span> <span class="c"># islice('ABCDEFG', 2) --> A B</span> <span class="c"># islice('ABCDEFG', 2, 4) --> C D</span> <span class="c"># islice('ABCDEFG', 2, None) --> C D E F G</span> <span class="c"># islice('ABCDEFG', 0, None, 2) --> A C E G</span> <span class="n">s</span> <span class="o">=</span> <span class="nb">slice</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">)</span> <span class="n">it</span> <span class="o">=</span> <span class="nb">iter</span><span class="p">(</span><span class="nb">xrange</span><span class="p">(</span><span class="n">s</span><span class="o">.</span><span class="n">start</span> <span class="ow">or</span> <span class="mi">0</span><span class="p">,</span> <span class="n">s</span><span class="o">.</span><span class="n">stop</span> <span class="ow">or</span> <span class="n">sys</span><span class="o">.</span><span class="n">maxint</span><span class="p">,</span> <span class="n">s</span><span class="o">.</span><span class="n">step</span> <span class="ow">or</span> <span class="mi">1</span><span class="p">))</span> <span class="n">nexti</span> <span class="o">=</span> <span class="nb">next</span><span class="p">(</span><span class="n">it</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">element</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">iterable</span><span class="p">):</span> <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="n">nexti</span><span class="p">:</span> <span class="k">yield</span> <span class="n">element</span> <span class="n">nexti</span> <span class="o">=</span> <span class="nb">next</span><span class="p">(</span><span class="n">it</span><span class="p">)</span> </pre></div> </div> <p>If <em>start</em> is <tt class="docutils literal"><span class="pre">None</span></tt>, then iteration starts at zero. If <em>step</em> is <tt class="docutils literal"><span class="pre">None</span></tt>, then the step defaults to one.</p> <p class="versionchanged"> <span class="versionmodified">Changed in version 2.5: </span>accept <tt class="docutils literal"><span class="pre">None</span></tt> values for default <em>start</em> and <em>step</em>.</p> </dd></dl> <dl class="function"> <dt id="itertools.izip"> <tt class="descclassname">itertools.</tt><tt class="descname">izip</tt><big>(</big><em>*iterables</em><big>)</big><a class="headerlink" href="#itertools.izip" title="Permalink to this definition">¶</a></dt> <dd><p>Make an iterator that aggregates elements from each of the iterables. Like <a class="reference internal" href="functions.html#zip" title="zip"><tt class="xref py py-func docutils literal"><span class="pre">zip()</span></tt></a> except that it returns an iterator instead of a list. Used for lock-step iteration over several iterables at a time. Equivalent to:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">izip</span><span class="p">(</span><span class="o">*</span><span class="n">iterables</span><span class="p">):</span> <span class="c"># izip('ABCD', 'xy') --> Ax By</span> <span class="n">iterators</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="nb">iter</span><span class="p">,</span> <span class="n">iterables</span><span class="p">)</span> <span class="k">while</span> <span class="n">iterators</span><span class="p">:</span> <span class="k">yield</span> <span class="nb">tuple</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="nb">next</span><span class="p">,</span> <span class="n">iterators</span><span class="p">))</span> </pre></div> </div> <p class="versionchanged"> <span class="versionmodified">Changed in version 2.4: </span>When no iterables are specified, returns a zero length iterator instead of raising a <a class="reference internal" href="exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> exception.</p> <p>The left-to-right evaluation order of the iterables is guaranteed. This makes possible an idiom for clustering a data series into n-length groups using <tt class="docutils literal"><span class="pre">izip(*[iter(s)]*n)</span></tt>.</p> <p><a class="reference internal" href="#itertools.izip" title="itertools.izip"><tt class="xref py py-func docutils literal"><span class="pre">izip()</span></tt></a> should only be used with unequal length inputs when you don’t care about trailing, unmatched values from the longer iterables. If those values are important, use <a class="reference internal" href="#itertools.izip_longest" title="itertools.izip_longest"><tt class="xref py py-func docutils literal"><span class="pre">izip_longest()</span></tt></a> instead.</p> </dd></dl> <dl class="function"> <dt id="itertools.izip_longest"> <tt class="descclassname">itertools.</tt><tt class="descname">izip_longest</tt><big>(</big><em>*iterables</em><span class="optional">[</span>, <em>fillvalue</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#itertools.izip_longest" title="Permalink to this definition">¶</a></dt> <dd><p>Make an iterator that aggregates elements from each of the iterables. If the iterables are of uneven length, missing values are filled-in with <em>fillvalue</em>. Iteration continues until the longest iterable is exhausted. Equivalent to:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">ZipExhausted</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span> <span class="k">pass</span> <span class="k">def</span> <span class="nf">izip_longest</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">):</span> <span class="c"># izip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-</span> <span class="n">fillvalue</span> <span class="o">=</span> <span class="n">kwds</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'fillvalue'</span><span class="p">)</span> <span class="n">counter</span> <span class="o">=</span> <span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">)</span> <span class="o">-</span> <span class="mi">1</span><span class="p">]</span> <span class="k">def</span> <span class="nf">sentinel</span><span class="p">():</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">counter</span><span class="p">[</span><span class="mi">0</span><span class="p">]:</span> <span class="k">raise</span> <span class="n">ZipExhausted</span> <span class="n">counter</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">-=</span> <span class="mi">1</span> <span class="k">yield</span> <span class="n">fillvalue</span> <span class="n">fillers</span> <span class="o">=</span> <span class="n">repeat</span><span class="p">(</span><span class="n">fillvalue</span><span class="p">)</span> <span class="n">iterators</span> <span class="o">=</span> <span class="p">[</span><span class="n">chain</span><span class="p">(</span><span class="n">it</span><span class="p">,</span> <span class="n">sentinel</span><span class="p">(),</span> <span class="n">fillers</span><span class="p">)</span> <span class="k">for</span> <span class="n">it</span> <span class="ow">in</span> <span class="n">args</span><span class="p">]</span> <span class="k">try</span><span class="p">:</span> <span class="k">while</span> <span class="n">iterators</span><span class="p">:</span> <span class="k">yield</span> <span class="nb">tuple</span><span class="p">(</span><span class="nb">map</span><span class="p">(</span><span class="nb">next</span><span class="p">,</span> <span class="n">iterators</span><span class="p">))</span> <span class="k">except</span> <span class="n">ZipExhausted</span><span class="p">:</span> <span class="k">pass</span> </pre></div> </div> <p>If one of the iterables is potentially infinite, then the <a class="reference internal" href="#itertools.izip_longest" title="itertools.izip_longest"><tt class="xref py py-func docutils literal"><span class="pre">izip_longest()</span></tt></a> function should be wrapped with something that limits the number of calls (for example <a class="reference internal" href="#itertools.islice" title="itertools.islice"><tt class="xref py py-func docutils literal"><span class="pre">islice()</span></tt></a> or <a class="reference internal" href="#itertools.takewhile" title="itertools.takewhile"><tt class="xref py py-func docutils literal"><span class="pre">takewhile()</span></tt></a>). If not specified, <em>fillvalue</em> defaults to <tt class="docutils literal"><span class="pre">None</span></tt>.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="function"> <dt id="itertools.permutations"> <tt class="descclassname">itertools.</tt><tt class="descname">permutations</tt><big>(</big><em>iterable</em><span class="optional">[</span>, <em>r</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#itertools.permutations" title="Permalink to this definition">¶</a></dt> <dd><p>Return successive <em>r</em> length permutations of elements in the <em>iterable</em>.</p> <p>If <em>r</em> is not specified or is <tt class="docutils literal"><span class="pre">None</span></tt>, then <em>r</em> defaults to the length of the <em>iterable</em> and all possible full-length permutations are generated.</p> <p>Permutations are emitted in lexicographic sort order. So, if the input <em>iterable</em> is sorted, the permutation tuples will be produced in sorted order.</p> <p>Elements are treated as unique based on their position, not on their value. So if the input elements are unique, there will be no repeat values in each permutation.</p> <p>Equivalent to:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">permutations</span><span class="p">(</span><span class="n">iterable</span><span class="p">,</span> <span class="n">r</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span> <span class="c"># permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC</span> <span class="c"># permutations(range(3)) --> 012 021 102 120 201 210</span> <span class="n">pool</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">iterable</span><span class="p">)</span> <span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">pool</span><span class="p">)</span> <span class="n">r</span> <span class="o">=</span> <span class="n">n</span> <span class="k">if</span> <span class="n">r</span> <span class="ow">is</span> <span class="bp">None</span> <span class="k">else</span> <span class="n">r</span> <span class="k">if</span> <span class="n">r</span> <span class="o">></span> <span class="n">n</span><span class="p">:</span> <span class="k">return</span> <span class="n">indices</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">)</span> <span class="n">cycles</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">n</span><span class="o">-</span><span class="n">r</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="k">yield</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">pool</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">indices</span><span class="p">[:</span><span class="n">r</span><span class="p">])</span> <span class="k">while</span> <span class="n">n</span><span class="p">:</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">reversed</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="n">r</span><span class="p">)):</span> <span class="n">cycles</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">-=</span> <span class="mi">1</span> <span class="k">if</span> <span class="n">cycles</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span> <span class="n">indices</span><span class="p">[</span><span class="n">i</span><span class="p">:]</span> <span class="o">=</span> <span class="n">indices</span><span class="p">[</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">:]</span> <span class="o">+</span> <span class="n">indices</span><span class="p">[</span><span class="n">i</span><span class="p">:</span><span class="n">i</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="n">cycles</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">n</span> <span class="o">-</span> <span class="n">i</span> <span class="k">else</span><span class="p">:</span> <span class="n">j</span> <span class="o">=</span> <span class="n">cycles</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="n">indices</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">indices</span><span class="p">[</span><span class="o">-</span><span class="n">j</span><span class="p">]</span> <span class="o">=</span> <span class="n">indices</span><span class="p">[</span><span class="o">-</span><span class="n">j</span><span class="p">],</span> <span class="n">indices</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">yield</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">pool</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">indices</span><span class="p">[:</span><span class="n">r</span><span class="p">])</span> <span class="k">break</span> <span class="k">else</span><span class="p">:</span> <span class="k">return</span> </pre></div> </div> <p>The code for <a class="reference internal" href="#itertools.permutations" title="itertools.permutations"><tt class="xref py py-func docutils literal"><span class="pre">permutations()</span></tt></a> can be also expressed as a subsequence of <a class="reference internal" href="#itertools.product" title="itertools.product"><tt class="xref py py-func docutils literal"><span class="pre">product()</span></tt></a>, filtered to exclude entries with repeated elements (those from the same position in the input pool):</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">permutations</span><span class="p">(</span><span class="n">iterable</span><span class="p">,</span> <span class="n">r</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span> <span class="n">pool</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">iterable</span><span class="p">)</span> <span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">pool</span><span class="p">)</span> <span class="n">r</span> <span class="o">=</span> <span class="n">n</span> <span class="k">if</span> <span class="n">r</span> <span class="ow">is</span> <span class="bp">None</span> <span class="k">else</span> <span class="n">r</span> <span class="k">for</span> <span class="n">indices</span> <span class="ow">in</span> <span class="n">product</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">),</span> <span class="n">repeat</span><span class="o">=</span><span class="n">r</span><span class="p">):</span> <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">indices</span><span class="p">))</span> <span class="o">==</span> <span class="n">r</span><span class="p">:</span> <span class="k">yield</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">pool</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">indices</span><span class="p">)</span> </pre></div> </div> <p>The number of items returned is <tt class="docutils literal"><span class="pre">n!</span> <span class="pre">/</span> <span class="pre">(n-r)!</span></tt> when <tt class="docutils literal"><span class="pre">0</span> <span class="pre"><=</span> <span class="pre">r</span> <span class="pre"><=</span> <span class="pre">n</span></tt> or zero when <tt class="docutils literal"><span class="pre">r</span> <span class="pre">></span> <span class="pre">n</span></tt>.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="function"> <dt id="itertools.product"> <tt class="descclassname">itertools.</tt><tt class="descname">product</tt><big>(</big><em>*iterables</em><span class="optional">[</span>, <em>repeat</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#itertools.product" title="Permalink to this definition">¶</a></dt> <dd><p>Cartesian product of input iterables.</p> <p>Equivalent to nested for-loops in a generator expression. For example, <tt class="docutils literal"><span class="pre">product(A,</span> <span class="pre">B)</span></tt> returns the same as <tt class="docutils literal"><span class="pre">((x,y)</span> <span class="pre">for</span> <span class="pre">x</span> <span class="pre">in</span> <span class="pre">A</span> <span class="pre">for</span> <span class="pre">y</span> <span class="pre">in</span> <span class="pre">B)</span></tt>.</p> <p>The nested loops cycle like an odometer with the rightmost element advancing on every iteration. This pattern creates a lexicographic ordering so that if the input’s iterables are sorted, the product tuples are emitted in sorted order.</p> <p>To compute the product of an iterable with itself, specify the number of repetitions with the optional <em>repeat</em> keyword argument. For example, <tt class="docutils literal"><span class="pre">product(A,</span> <span class="pre">repeat=4)</span></tt> means the same as <tt class="docutils literal"><span class="pre">product(A,</span> <span class="pre">A,</span> <span class="pre">A,</span> <span class="pre">A)</span></tt>.</p> <p>This function is equivalent to the following code, except that the actual implementation does not build up intermediate results in memory:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">product</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">):</span> <span class="c"># product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy</span> <span class="c"># product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111</span> <span class="n">pools</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="nb">tuple</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span> <span class="o">*</span> <span class="n">kwds</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'repeat'</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="n">result</span> <span class="o">=</span> <span class="p">[[]]</span> <span class="k">for</span> <span class="n">pool</span> <span class="ow">in</span> <span class="n">pools</span><span class="p">:</span> <span class="n">result</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span><span class="o">+</span><span class="p">[</span><span class="n">y</span><span class="p">]</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">result</span> <span class="k">for</span> <span class="n">y</span> <span class="ow">in</span> <span class="n">pool</span><span class="p">]</span> <span class="k">for</span> <span class="n">prod</span> <span class="ow">in</span> <span class="n">result</span><span class="p">:</span> <span class="k">yield</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">prod</span><span class="p">)</span> </pre></div> </div> <p class="versionadded"> <span class="versionmodified">New in version 2.6.</span></p> </dd></dl> <dl class="function"> <dt id="itertools.repeat"> <tt class="descclassname">itertools.</tt><tt class="descname">repeat</tt><big>(</big><em>object</em><span class="optional">[</span>, <em>times</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#itertools.repeat" title="Permalink to this definition">¶</a></dt> <dd><p>Make an iterator that returns <em>object</em> over and over again. Runs indefinitely unless the <em>times</em> argument is specified. Used as argument to <a class="reference internal" href="#itertools.imap" title="itertools.imap"><tt class="xref py py-func docutils literal"><span class="pre">imap()</span></tt></a> for invariant function parameters. Also used with <a class="reference internal" href="#itertools.izip" title="itertools.izip"><tt class="xref py py-func docutils literal"><span class="pre">izip()</span></tt></a> to create constant fields in a tuple record. Equivalent to:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">repeat</span><span class="p">(</span><span class="nb">object</span><span class="p">,</span> <span class="n">times</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span> <span class="c"># repeat(10, 3) --> 10 10 10</span> <span class="k">if</span> <span class="n">times</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="k">while</span> <span class="bp">True</span><span class="p">:</span> <span class="k">yield</span> <span class="nb">object</span> <span class="k">else</span><span class="p">:</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="n">times</span><span class="p">):</span> <span class="k">yield</span> <span class="nb">object</span> </pre></div> </div> <p>A common use for <em>repeat</em> is to supply a stream of constant values to <em>imap</em> or <em>zip</em>:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="nb">list</span><span class="p">(</span><span class="n">imap</span><span class="p">(</span><span class="nb">pow</span><span class="p">,</span> <span class="nb">xrange</span><span class="p">(</span><span class="mi">10</span><span class="p">),</span> <span class="n">repeat</span><span class="p">(</span><span class="mi">2</span><span class="p">)))</span> <span class="go">[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]</span> </pre></div> </div> </dd></dl> <dl class="function"> <dt id="itertools.starmap"> <tt class="descclassname">itertools.</tt><tt class="descname">starmap</tt><big>(</big><em>function</em>, <em>iterable</em><big>)</big><a class="headerlink" href="#itertools.starmap" title="Permalink to this definition">¶</a></dt> <dd><p>Make an iterator that computes the function using arguments obtained from the iterable. Used instead of <a class="reference internal" href="#itertools.imap" title="itertools.imap"><tt class="xref py py-func docutils literal"><span class="pre">imap()</span></tt></a> when argument parameters are already grouped in tuples from a single iterable (the data has been “pre-zipped”). The difference between <a class="reference internal" href="#itertools.imap" title="itertools.imap"><tt class="xref py py-func docutils literal"><span class="pre">imap()</span></tt></a> and <a class="reference internal" href="#itertools.starmap" title="itertools.starmap"><tt class="xref py py-func docutils literal"><span class="pre">starmap()</span></tt></a> parallels the distinction between <tt class="docutils literal"><span class="pre">function(a,b)</span></tt> and <tt class="docutils literal"><span class="pre">function(*c)</span></tt>. Equivalent to:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">starmap</span><span class="p">(</span><span class="n">function</span><span class="p">,</span> <span class="n">iterable</span><span class="p">):</span> <span class="c"># starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000</span> <span class="k">for</span> <span class="n">args</span> <span class="ow">in</span> <span class="n">iterable</span><span class="p">:</span> <span class="k">yield</span> <span class="n">function</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">)</span> </pre></div> </div> <p class="versionchanged"> <span class="versionmodified">Changed in version 2.6: </span>Previously, <a class="reference internal" href="#itertools.starmap" title="itertools.starmap"><tt class="xref py py-func docutils literal"><span class="pre">starmap()</span></tt></a> required the function arguments to be tuples. Now, any iterable is allowed.</p> </dd></dl> <dl class="function"> <dt id="itertools.takewhile"> <tt class="descclassname">itertools.</tt><tt class="descname">takewhile</tt><big>(</big><em>predicate</em>, <em>iterable</em><big>)</big><a class="headerlink" href="#itertools.takewhile" title="Permalink to this definition">¶</a></dt> <dd><p>Make an iterator that returns elements from the iterable as long as the predicate is true. Equivalent to:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">takewhile</span><span class="p">(</span><span class="n">predicate</span><span class="p">,</span> <span class="n">iterable</span><span class="p">):</span> <span class="c"># takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">iterable</span><span class="p">:</span> <span class="k">if</span> <span class="n">predicate</span><span class="p">(</span><span class="n">x</span><span class="p">):</span> <span class="k">yield</span> <span class="n">x</span> <span class="k">else</span><span class="p">:</span> <span class="k">break</span> </pre></div> </div> </dd></dl> <dl class="function"> <dt id="itertools.tee"> <tt class="descclassname">itertools.</tt><tt class="descname">tee</tt><big>(</big><em>iterable</em><span class="optional">[</span>, <em>n=2</em><span class="optional">]</span><big>)</big><a class="headerlink" href="#itertools.tee" title="Permalink to this definition">¶</a></dt> <dd><p>Return <em>n</em> independent iterators from a single iterable. Equivalent to:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">tee</span><span class="p">(</span><span class="n">iterable</span><span class="p">,</span> <span class="n">n</span><span class="o">=</span><span class="mi">2</span><span class="p">):</span> <span class="n">it</span> <span class="o">=</span> <span class="nb">iter</span><span class="p">(</span><span class="n">iterable</span><span class="p">)</span> <span class="n">deques</span> <span class="o">=</span> <span class="p">[</span><span class="n">collections</span><span class="o">.</span><span class="n">deque</span><span class="p">()</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n</span><span class="p">)]</span> <span class="k">def</span> <span class="nf">gen</span><span class="p">(</span><span class="n">mydeque</span><span class="p">):</span> <span class="k">while</span> <span class="bp">True</span><span class="p">:</span> <span class="k">if</span> <span class="ow">not</span> <span class="n">mydeque</span><span class="p">:</span> <span class="c"># when the local deque is empty</span> <span class="n">newval</span> <span class="o">=</span> <span class="nb">next</span><span class="p">(</span><span class="n">it</span><span class="p">)</span> <span class="c"># fetch a new value and</span> <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">deques</span><span class="p">:</span> <span class="c"># load it to all the deques</span> <span class="n">d</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">newval</span><span class="p">)</span> <span class="k">yield</span> <span class="n">mydeque</span><span class="o">.</span><span class="n">popleft</span><span class="p">()</span> <span class="k">return</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">gen</span><span class="p">(</span><span class="n">d</span><span class="p">)</span> <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">deques</span><span class="p">)</span> </pre></div> </div> <p>Once <a class="reference internal" href="#itertools.tee" title="itertools.tee"><tt class="xref py py-func docutils literal"><span class="pre">tee()</span></tt></a> has made a split, the original <em>iterable</em> should not be used anywhere else; otherwise, the <em>iterable</em> could get advanced without the tee objects being informed.</p> <p>This itertool may require significant auxiliary storage (depending on how much temporary data needs to be stored). In general, if one iterator uses most or all of the data before another iterator starts, it is faster to use <a class="reference internal" href="functions.html#list" title="list"><tt class="xref py py-func docutils literal"><span class="pre">list()</span></tt></a> instead of <a class="reference internal" href="#itertools.tee" title="itertools.tee"><tt class="xref py py-func docutils literal"><span class="pre">tee()</span></tt></a>.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.4.</span></p> </dd></dl> </div> <div class="section" id="recipes"> <span id="itertools-recipes"></span><h2>9.7.2. Recipes<a class="headerlink" href="#recipes" title="Permalink to this headline">¶</a></h2> <p>This section shows recipes for creating an extended toolset using the existing itertools as building blocks.</p> <p>The extended tools offer the same high performance as the underlying toolset. The superior memory performance is kept by processing elements one at a time rather than bringing the whole iterable into memory all at once. Code volume is kept small by linking the tools together in a functional style which helps eliminate temporary variables. High speed is retained by preferring “vectorized” building blocks over the use of for-loops and <a class="reference internal" href="../glossary.html#term-generator"><em class="xref std std-term">generator</em></a>s which incur interpreter overhead.</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">take</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">iterable</span><span class="p">):</span> <span class="s">"Return first n items of the iterable as a list"</span> <span class="k">return</span> <span class="nb">list</span><span class="p">(</span><span class="n">islice</span><span class="p">(</span><span class="n">iterable</span><span class="p">,</span> <span class="n">n</span><span class="p">))</span> <span class="k">def</span> <span class="nf">tabulate</span><span class="p">(</span><span class="n">function</span><span class="p">,</span> <span class="n">start</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span> <span class="s">"Return function(0), function(1), ..."</span> <span class="k">return</span> <span class="n">imap</span><span class="p">(</span><span class="n">function</span><span class="p">,</span> <span class="n">count</span><span class="p">(</span><span class="n">start</span><span class="p">))</span> <span class="k">def</span> <span class="nf">consume</span><span class="p">(</span><span class="n">iterator</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span> <span class="s">"Advance the iterator n-steps ahead. If n is none, consume entirely."</span> <span class="c"># Use functions that consume iterators at C speed.</span> <span class="k">if</span> <span class="n">n</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="c"># feed the entire iterator into a zero-length deque</span> <span class="n">collections</span><span class="o">.</span><span class="n">deque</span><span class="p">(</span><span class="n">iterator</span><span class="p">,</span> <span class="n">maxlen</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span> <span class="k">else</span><span class="p">:</span> <span class="c"># advance to the empty slice starting at position n</span> <span class="nb">next</span><span class="p">(</span><span class="n">islice</span><span class="p">(</span><span class="n">iterator</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">n</span><span class="p">),</span> <span class="bp">None</span><span class="p">)</span> <span class="k">def</span> <span class="nf">nth</span><span class="p">(</span><span class="n">iterable</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span> <span class="s">"Returns the nth item or a default value"</span> <span class="k">return</span> <span class="nb">next</span><span class="p">(</span><span class="n">islice</span><span class="p">(</span><span class="n">iterable</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="bp">None</span><span class="p">),</span> <span class="n">default</span><span class="p">)</span> <span class="k">def</span> <span class="nf">quantify</span><span class="p">(</span><span class="n">iterable</span><span class="p">,</span> <span class="n">pred</span><span class="o">=</span><span class="nb">bool</span><span class="p">):</span> <span class="s">"Count how many times the predicate is true"</span> <span class="k">return</span> <span class="nb">sum</span><span class="p">(</span><span class="n">imap</span><span class="p">(</span><span class="n">pred</span><span class="p">,</span> <span class="n">iterable</span><span class="p">))</span> <span class="k">def</span> <span class="nf">padnone</span><span class="p">(</span><span class="n">iterable</span><span class="p">):</span> <span class="sd">"""Returns the sequence elements and then returns None indefinitely.</span> <span class="sd"> Useful for emulating the behavior of the built-in map() function.</span> <span class="sd"> """</span> <span class="k">return</span> <span class="n">chain</span><span class="p">(</span><span class="n">iterable</span><span class="p">,</span> <span class="n">repeat</span><span class="p">(</span><span class="bp">None</span><span class="p">))</span> <span class="k">def</span> <span class="nf">ncycles</span><span class="p">(</span><span class="n">iterable</span><span class="p">,</span> <span class="n">n</span><span class="p">):</span> <span class="s">"Returns the sequence elements n times"</span> <span class="k">return</span> <span class="n">chain</span><span class="o">.</span><span class="n">from_iterable</span><span class="p">(</span><span class="n">repeat</span><span class="p">(</span><span class="nb">tuple</span><span class="p">(</span><span class="n">iterable</span><span class="p">),</span> <span class="n">n</span><span class="p">))</span> <span class="k">def</span> <span class="nf">dotproduct</span><span class="p">(</span><span class="n">vec1</span><span class="p">,</span> <span class="n">vec2</span><span class="p">):</span> <span class="k">return</span> <span class="nb">sum</span><span class="p">(</span><span class="n">imap</span><span class="p">(</span><span class="n">operator</span><span class="o">.</span><span class="n">mul</span><span class="p">,</span> <span class="n">vec1</span><span class="p">,</span> <span class="n">vec2</span><span class="p">))</span> <span class="k">def</span> <span class="nf">flatten</span><span class="p">(</span><span class="n">listOfLists</span><span class="p">):</span> <span class="s">"Flatten one level of nesting"</span> <span class="k">return</span> <span class="n">chain</span><span class="o">.</span><span class="n">from_iterable</span><span class="p">(</span><span class="n">listOfLists</span><span class="p">)</span> <span class="k">def</span> <span class="nf">repeatfunc</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">times</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span> <span class="sd">"""Repeat calls to func with specified arguments.</span> <span class="sd"> Example: repeatfunc(random.random)</span> <span class="sd"> """</span> <span class="k">if</span> <span class="n">times</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="k">return</span> <span class="n">starmap</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">repeat</span><span class="p">(</span><span class="n">args</span><span class="p">))</span> <span class="k">return</span> <span class="n">starmap</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">repeat</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="n">times</span><span class="p">))</span> <span class="k">def</span> <span class="nf">pairwise</span><span class="p">(</span><span class="n">iterable</span><span class="p">):</span> <span class="s">"s -> (s0,s1), (s1,s2), (s2, s3), ..."</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span> <span class="o">=</span> <span class="n">tee</span><span class="p">(</span><span class="n">iterable</span><span class="p">)</span> <span class="nb">next</span><span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span> <span class="k">return</span> <span class="n">izip</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">)</span> <span class="k">def</span> <span class="nf">grouper</span><span class="p">(</span><span class="n">iterable</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">fillvalue</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span> <span class="s">"Collect data into fixed-length chunks or blocks"</span> <span class="c"># grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx</span> <span class="n">args</span> <span class="o">=</span> <span class="p">[</span><span class="nb">iter</span><span class="p">(</span><span class="n">iterable</span><span class="p">)]</span> <span class="o">*</span> <span class="n">n</span> <span class="k">return</span> <span class="n">izip_longest</span><span class="p">(</span><span class="n">fillvalue</span><span class="o">=</span><span class="n">fillvalue</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">)</span> <span class="k">def</span> <span class="nf">roundrobin</span><span class="p">(</span><span class="o">*</span><span class="n">iterables</span><span class="p">):</span> <span class="s">"roundrobin('ABC', 'D', 'EF') --> A D E B F C"</span> <span class="c"># Recipe credited to George Sakkis</span> <span class="n">pending</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">iterables</span><span class="p">)</span> <span class="n">nexts</span> <span class="o">=</span> <span class="n">cycle</span><span class="p">(</span><span class="nb">iter</span><span class="p">(</span><span class="n">it</span><span class="p">)</span><span class="o">.</span><span class="n">next</span> <span class="k">for</span> <span class="n">it</span> <span class="ow">in</span> <span class="n">iterables</span><span class="p">)</span> <span class="k">while</span> <span class="n">pending</span><span class="p">:</span> <span class="k">try</span><span class="p">:</span> <span class="k">for</span> <span class="nb">next</span> <span class="ow">in</span> <span class="n">nexts</span><span class="p">:</span> <span class="k">yield</span> <span class="nb">next</span><span class="p">()</span> <span class="k">except</span> <span class="ne">StopIteration</span><span class="p">:</span> <span class="n">pending</span> <span class="o">-=</span> <span class="mi">1</span> <span class="n">nexts</span> <span class="o">=</span> <span class="n">cycle</span><span class="p">(</span><span class="n">islice</span><span class="p">(</span><span class="n">nexts</span><span class="p">,</span> <span class="n">pending</span><span class="p">))</span> <span class="k">def</span> <span class="nf">powerset</span><span class="p">(</span><span class="n">iterable</span><span class="p">):</span> <span class="s">"powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"</span> <span class="n">s</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">iterable</span><span class="p">)</span> <span class="k">return</span> <span class="n">chain</span><span class="o">.</span><span class="n">from_iterable</span><span class="p">(</span><span class="n">combinations</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">r</span><span class="p">)</span> <span class="k">for</span> <span class="n">r</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">))</span> <span class="k">def</span> <span class="nf">unique_everseen</span><span class="p">(</span><span class="n">iterable</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span> <span class="s">"List unique elements, preserving order. Remember all elements ever seen."</span> <span class="c"># unique_everseen('AAAABBBCCDAABBB') --> A B C D</span> <span class="c"># unique_everseen('ABBCcAD', str.lower) --> A B C D</span> <span class="n">seen</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span> <span class="n">seen_add</span> <span class="o">=</span> <span class="n">seen</span><span class="o">.</span><span class="n">add</span> <span class="k">if</span> <span class="n">key</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span> <span class="k">for</span> <span class="n">element</span> <span class="ow">in</span> <span class="n">ifilterfalse</span><span class="p">(</span><span class="n">seen</span><span class="o">.</span><span class="n">__contains__</span><span class="p">,</span> <span class="n">iterable</span><span class="p">):</span> <span class="n">seen_add</span><span class="p">(</span><span class="n">element</span><span class="p">)</span> <span class="k">yield</span> <span class="n">element</span> <span class="k">else</span><span class="p">:</span> <span class="k">for</span> <span class="n">element</span> <span class="ow">in</span> <span class="n">iterable</span><span class="p">:</span> <span class="n">k</span> <span class="o">=</span> <span class="n">key</span><span class="p">(</span><span class="n">element</span><span class="p">)</span> <span class="k">if</span> <span class="n">k</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">seen</span><span class="p">:</span> <span class="n">seen_add</span><span class="p">(</span><span class="n">k</span><span class="p">)</span> <span class="k">yield</span> <span class="n">element</span> <span class="k">def</span> <span class="nf">unique_justseen</span><span class="p">(</span><span class="n">iterable</span><span class="p">,</span> <span class="n">key</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span> <span class="s">"List unique elements, preserving order. Remember only the element just seen."</span> <span class="c"># unique_justseen('AAAABBBCCDAABBB') --> A B C D A B</span> <span class="c"># unique_justseen('ABBCcAD', str.lower) --> A B C A D</span> <span class="k">return</span> <span class="n">imap</span><span class="p">(</span><span class="nb">next</span><span class="p">,</span> <span class="n">imap</span><span class="p">(</span><span class="n">itemgetter</span><span class="p">(</span><span class="mi">1</span><span class="p">),</span> <span class="n">groupby</span><span class="p">(</span><span class="n">iterable</span><span class="p">,</span> <span class="n">key</span><span class="p">)))</span> <span class="k">def</span> <span class="nf">iter_except</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">exception</span><span class="p">,</span> <span class="n">first</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span> <span class="sd">""" Call a function repeatedly until an exception is raised.</span> <span class="sd"> Converts a call-until-exception interface to an iterator interface.</span> <span class="sd"> Like __builtin__.iter(func, sentinel) but uses an exception instead</span> <span class="sd"> of a sentinel to end the loop.</span> <span class="sd"> Examples:</span> <span class="sd"> bsddbiter = iter_except(db.next, bsddb.error, db.first)</span> <span class="sd"> heapiter = iter_except(functools.partial(heappop, h), IndexError)</span> <span class="sd"> dictiter = iter_except(d.popitem, KeyError)</span> <span class="sd"> dequeiter = iter_except(d.popleft, IndexError)</span> <span class="sd"> queueiter = iter_except(q.get_nowait, Queue.Empty)</span> <span class="sd"> setiter = iter_except(s.pop, KeyError)</span> <span class="sd"> """</span> <span class="k">try</span><span class="p">:</span> <span class="k">if</span> <span class="n">first</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span> <span class="k">yield</span> <span class="n">first</span><span class="p">()</span> <span class="k">while</span> <span class="mi">1</span><span class="p">:</span> <span class="k">yield</span> <span class="n">func</span><span class="p">()</span> <span class="k">except</span> <span class="n">exception</span><span class="p">:</span> <span class="k">pass</span> <span class="k">def</span> <span class="nf">random_product</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwds</span><span class="p">):</span> <span class="s">"Random selection from itertools.product(*args, **kwds)"</span> <span class="n">pools</span> <span class="o">=</span> <span class="nb">map</span><span class="p">(</span><span class="nb">tuple</span><span class="p">,</span> <span class="n">args</span><span class="p">)</span> <span class="o">*</span> <span class="n">kwds</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'repeat'</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="k">return</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">choice</span><span class="p">(</span><span class="n">pool</span><span class="p">)</span> <span class="k">for</span> <span class="n">pool</span> <span class="ow">in</span> <span class="n">pools</span><span class="p">)</span> <span class="k">def</span> <span class="nf">random_permutation</span><span class="p">(</span><span class="n">iterable</span><span class="p">,</span> <span class="n">r</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span> <span class="s">"Random selection from itertools.permutations(iterable, r)"</span> <span class="n">pool</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">iterable</span><span class="p">)</span> <span class="n">r</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">pool</span><span class="p">)</span> <span class="k">if</span> <span class="n">r</span> <span class="ow">is</span> <span class="bp">None</span> <span class="k">else</span> <span class="n">r</span> <span class="k">return</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="n">pool</span><span class="p">,</span> <span class="n">r</span><span class="p">))</span> <span class="k">def</span> <span class="nf">random_combination</span><span class="p">(</span><span class="n">iterable</span><span class="p">,</span> <span class="n">r</span><span class="p">):</span> <span class="s">"Random selection from itertools.combinations(iterable, r)"</span> <span class="n">pool</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">iterable</span><span class="p">)</span> <span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">pool</span><span class="p">)</span> <span class="n">indices</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">sample</span><span class="p">(</span><span class="nb">xrange</span><span class="p">(</span><span class="n">n</span><span class="p">),</span> <span class="n">r</span><span class="p">))</span> <span class="k">return</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">pool</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">indices</span><span class="p">)</span> <span class="k">def</span> <span class="nf">random_combination_with_replacement</span><span class="p">(</span><span class="n">iterable</span><span class="p">,</span> <span class="n">r</span><span class="p">):</span> <span class="s">"Random selection from itertools.combinations_with_replacement(iterable, r)"</span> <span class="n">pool</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">iterable</span><span class="p">)</span> <span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">pool</span><span class="p">)</span> <span class="n">indices</span> <span class="o">=</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">random</span><span class="o">.</span><span class="n">randrange</span><span class="p">(</span><span class="n">n</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">xrange</span><span class="p">(</span><span class="n">r</span><span class="p">))</span> <span class="k">return</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">pool</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="n">indices</span><span class="p">)</span> <span class="k">def</span> <span class="nf">tee_lookahead</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">i</span><span class="p">):</span> <span class="sd">"""Inspect the i-th upcomping value from a tee object</span> <span class="sd"> while leaving the tee object at its current position.</span> <span class="sd"> Raise an IndexError if the underlying iterator doesn't</span> <span class="sd"> have enough values.</span> <span class="sd"> """</span> <span class="k">for</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">islice</span><span class="p">(</span><span class="n">t</span><span class="o">.</span><span class="n">__copy__</span><span class="p">(),</span> <span class="n">i</span><span class="p">,</span> <span class="bp">None</span><span class="p">):</span> <span class="k">return</span> <span class="n">value</span> <span class="k">raise</span> <span class="ne">IndexError</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> </pre></div> </div> <p>Note, many of the above recipes can be optimized by replacing global lookups with local variables defined as default values. For example, the <em>dotproduct</em> recipe can be written as:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">dotproduct</span><span class="p">(</span><span class="n">vec1</span><span class="p">,</span> <span class="n">vec2</span><span class="p">,</span> <span class="nb">sum</span><span class="o">=</span><span class="nb">sum</span><span class="p">,</span> <span class="n">imap</span><span class="o">=</span><span class="n">imap</span><span class="p">,</span> <span class="n">mul</span><span class="o">=</span><span class="n">operator</span><span class="o">.</span><span class="n">mul</span><span class="p">):</span> <span class="k">return</span> <span class="nb">sum</span><span class="p">(</span><span class="n">imap</span><span class="p">(</span><span class="n">mul</span><span class="p">,</span> <span class="n">vec1</span><span class="p">,</span> <span class="n">vec2</span><span class="p">))</span> </pre></div> </div> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">9.7. <tt class="docutils literal"><span class="pre">itertools</span></tt> — Functions creating iterators for efficient looping</a><ul> <li><a class="reference internal" href="#itertool-functions">9.7.1. Itertool functions</a></li> <li><a class="reference internal" href="#recipes">9.7.2. Recipes</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="random.html" title="previous chapter">9.6. <tt class="docutils literal"><span class="pre">random</span></tt> — Generate pseudo-random numbers</a></p> <h4>Next topic</h4> <p class="topless"><a href="functools.html" title="next chapter">9.8. <tt class="docutils literal"><span class="pre">functools</span></tt> — Higher-order functions and operations on callable objects</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li><a href="../_sources/library/itertools.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="functools.html" title="9.8. functools — Higher-order functions and operations on callable objects" >next</a> |</li> <li class="right" > <a href="random.html" title="9.6. random — Generate pseudo-random numbers" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="numeric.html" >9. Numeric and Mathematical Modules</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2019, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="http://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Jul 03, 2019. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>
Close