{"id":25612,"date":"2021-11-23T08:54:26","date_gmt":"2021-11-23T03:24:26","guid":{"rendered":"https:\/\/python-programs.com\/?p=25612"},"modified":"2021-11-23T08:54:26","modified_gmt":"2021-11-23T03:24:26","slug":"python-itertools-permutations-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-itertools-permutations-function-with-examples\/","title":{"rendered":"Python Itertools.permutations() Function with Examples"},"content":{"rendered":"

Itertools Module:<\/strong><\/p>\n

Itertools is a Python module that contains a collection of functions for dealing with iterators. They make it very simple to iterate through iterables such as lists and strings.<\/p>\n

Itertools in Python refers to a Python module that allows the creation of iterators, which aids in efficient looping, as well as time and space efficiency. Itertools enable us to solve complex problems quickly and easily. Iterators are classified into three types.<\/p>\n

This module provides the following types of iterators:<\/p>\n

    \n
  1. Combinatorics Generators<\/li>\n
  2. Infinite Iterators<\/li>\n
  3. Terminating Iterators<\/li>\n<\/ol>\n

    itertools.permutations() Function:<\/strong><\/p>\n

    Itertools.permutations() function is part of the Combinatoric Generators class. Combinatoric iterators are recursive generators that are used to simplify combinatorial constructs such as permutations, combinations, and Cartesian products.<\/p>\n

    The term “permutation” refers to all of the possible ways in which a set or string can be ordered or arranged. Similarly, itertool. The permutations() method returns all possible arrangements for an iterator, and all elements are assumed to be unique based on their position rather than their value or category.<\/p>\n

    All of these permutations are listed in alphabetical order. The function itertools.permutations() accepts an iterator and ‘r’ (length of permutation required) as input and, if not specified, assumes ‘r’ as the default length of the iterator and returns all possible permutations of length ‘r’ each.<\/p>\n

    Syntax:<\/strong><\/p>\n

    permutations(iterator, r)<\/pre>\n

    Parameters<\/strong><\/p>\n

    iterator:<\/strong> This is Required. Elements whose cartesian product must be calculated.<\/p>\n

    r:<\/strong> This is Optional. The length of the outputs (number of iterables by default).<\/p>\n

    Examples:<\/strong><\/p>\n

    Example1:<\/strong><\/p>\n

    Input:<\/strong><\/p>\n

    Given string = \"hello\"\r\nGiven r value(length)= 3<\/pre>\n

    Output:<\/strong><\/p>\n

    ('h', 'e', 'l')\r\n('h', 'e', 'l')\r\n('h', 'e', 'o')\r\n('h', 'l', 'e')\r\n('h', 'l', 'l')\r\n('h', 'l', 'o')\r\n('h', 'l', 'e')\r\n('h', 'l', 'l')\r\n('h', 'l', 'o')\r\n('h', 'o', 'e')\r\n('h', 'o', 'l')\r\n('h', 'o', 'l')\r\n('e', 'h', 'l')\r\n('e', 'h', 'l')\r\n('e', 'h', 'o')\r\n('e', 'l', 'h')\r\n('e', 'l', 'l')\r\n('e', 'l', 'o')\r\n('e', 'l', 'h')\r\n('e', 'l', 'l')\r\n('e', 'l', 'o')\r\n('e', 'o', 'h')\r\n('e', 'o', 'l')\r\n('e', 'o', 'l')\r\n('l', 'h', 'e')\r\n('l', 'h', 'l')\r\n('l', 'h', 'o')\r\n('l', 'e', 'h')\r\n('l', 'e', 'l')\r\n('l', 'e', 'o')\r\n('l', 'l', 'h')\r\n('l', 'l', 'e')\r\n('l', 'l', 'o')\r\n('l', 'o', 'h')\r\n('l', 'o', 'e')\r\n('l', 'o', 'l')\r\n('l', 'h', 'e')\r\n('l', 'h', 'l')\r\n('l', 'h', 'o')\r\n('l', 'e', 'h')\r\n('l', 'e', 'l')\r\n('l', 'e', 'o')\r\n('l', 'l', 'h')\r\n('l', 'l', 'e')\r\n('l', 'l', 'o')\r\n('l', 'o', 'h')\r\n('l', 'o', 'e')\r\n('l', 'o', 'l')\r\n('o', 'h', 'e')\r\n('o', 'h', 'l')\r\n('o', 'h', 'l')\r\n('o', 'e', 'h')\r\n('o', 'e', 'l')\r\n('o', 'e', 'l')\r\n('o', 'l', 'h')\r\n('o', 'l', 'e')\r\n('o', 'l', 'l')\r\n('o', 'l', 'h')\r\n('o', 'l', 'e')\r\n('o', 'l', 'l')<\/pre>\n

    Example2:<\/strong><\/p>\n

    Input:<\/strong><\/p>\n

    Given list = [123, 'hello', 'all']\r\nGiven r value(length)= 3<\/pre>\n

    Output:<\/strong><\/p>\n

    The all given list's permutations are :\r\n[(123, 'hello', 'all'), (123, 'all', 'hello'), ('hello', 123, 'all'), ('hello', 'all', 123), ('all', 123, 'hello'), ('all', 'hello', 123)]<\/pre>\n

    itertools.permutations() Function with Examples in Python<\/h2>\n