.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/builtin_modules/_itertools.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code or to run this example in your browser via Binder .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_builtin_modules__itertools.py: ==================== 2.12 itertools ==================== .. important:: This lesson is still under development. .. GENERATED FROM PYTHON SOURCE LINES 10-14 .. code-block:: default import itertools .. GENERATED FROM PYTHON SOURCE LINES 15-17 combinations ------------- .. GENERATED FROM PYTHON SOURCE LINES 17-19 .. code-block:: default x = [1,2,3] .. GENERATED FROM PYTHON SOURCE LINES 20-22 .. code-block:: default print([x for x in itertools.combinations(x, 2)]) .. rst-class:: sphx-glr-script-out .. code-block:: none [(1, 2), (1, 3), (2, 3)] .. GENERATED FROM PYTHON SOURCE LINES 23-26 .. code-block:: default print([x for x in itertools.combinations(x, 3)]) .. rst-class:: sphx-glr-script-out .. code-block:: none [(1, 2, 3)] .. GENERATED FROM PYTHON SOURCE LINES 27-30 .. code-block:: default print([x for x in itertools.combinations(x, 10)]) .. rst-class:: sphx-glr-script-out .. code-block:: none [] .. GENERATED FROM PYTHON SOURCE LINES 31-33 product -------- .. GENERATED FROM PYTHON SOURCE LINES 33-37 .. code-block:: default for prod in (itertools.product([1, 2], [11, 12])): print(prod) .. rst-class:: sphx-glr-script-out .. code-block:: none (1, 11) (1, 12) (2, 11) (2, 12) .. GENERATED FROM PYTHON SOURCE LINES 38-42 .. code-block:: default for prod in (itertools.product([1,2,3], [11, 12, 13])): print(prod) .. rst-class:: sphx-glr-script-out .. code-block:: none (1, 11) (1, 12) (1, 13) (2, 11) (2, 12) (2, 13) (3, 11) (3, 12) (3, 13) .. GENERATED FROM PYTHON SOURCE LINES 43-46 .. code-block:: default for prod in (itertools.product([1, 2], [11, 12], [21, 23])): print(prod) .. rst-class:: sphx-glr-script-out .. code-block:: none (1, 11, 21) (1, 11, 23) (1, 12, 21) (1, 12, 23) (2, 11, 21) (2, 11, 23) (2, 12, 21) (2, 12, 23) .. GENERATED FROM PYTHON SOURCE LINES 47-49 permutations ------------- .. GENERATED FROM PYTHON SOURCE LINES 49-51 .. code-block:: default print([x for x in itertools.permutations([1,2,3])]) .. rst-class:: sphx-glr-script-out .. code-block:: none [(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)] .. GENERATED FROM PYTHON SOURCE LINES 52-55 .. code-block:: default print([x for x in itertools.permutations([1,2,3], 2)]) .. rst-class:: sphx-glr-script-out .. code-block:: none [(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)] .. GENERATED FROM PYTHON SOURCE LINES 56-59 repeat ------- Repeats a value n times .. GENERATED FROM PYTHON SOURCE LINES 59-66 .. code-block:: default value = 10 n = 4 rep = itertools.repeat(value, n) print(rep) .. rst-class:: sphx-glr-script-out .. code-block:: none repeat(10, 4) .. GENERATED FROM PYTHON SOURCE LINES 67-69 .. code-block:: default print(type(rep)) .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 70-73 .. code-block:: default for val in rep: print(val) .. rst-class:: sphx-glr-script-out .. code-block:: none 10 10 10 10 .. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.005 seconds) .. _sphx_glr_download_auto_examples_builtin_modules__itertools.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: binder-badge .. image:: images/binder_badge_logo.svg :target: https://mybinder.org/v2/gh/AtrCheema/python-seekho/master?urlpath=lab/tree/notebooks/auto_examples/builtin_modules/_itertools.ipynb :alt: Launch binder :width: 150 px .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: _itertools.py <_itertools.py>` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: _itertools.ipynb <_itertools.ipynb>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_