A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/python-itertools-permutations/ below:

Itertools.Permutations() - Python - GeeksforGeeks

The permutations() function in Python, part of the itertools module, generates all possible ordered arrangements of a given iterable (like a list, string, or tuple). Unlike combinations, where order doesn't matter, permutations consider the order of elements. It returns an iterator producing tuples, each representing a unique permutation of the input elements. Example:

Python
from itertools import permutations
a = "GeEK"

# generate and print all permutations
for j in permutations(a):
    print(j)

Output

('G', 'e', 'E', 'K')
('G', 'e', 'K', 'E')
('G', 'E', 'e', 'K')
('G', 'E', 'K', 'e')
('G', 'K', 'e', 'E')
('G', 'K', 'E', 'e')
('e', 'G', 'E', 'K')
('e', 'G', 'K', 'E')
('e', 'E', 'G', 'K')
('e', 'E', 'K', 'G')
('e', 'K', 'G', 'E')
('e', 'K', 'E', 'G')
('E', 'G', 'e', 'K')
('E', 'G', 'K', 'e')
('E', 'e', 'G', 'K')
('E', 'e', 'K', 'G')
('E', 'K', 'G', 'e')
('E', 'K', 'e', 'G')
('K', 'G', 'e', 'E')
('K', 'G', 'E', 'e')
('K', 'e', 'G', 'E')
('K', 'e', 'E', 'G')
('K', 'E', 'G', 'e')
('K', 'E', 'e', 'G')

Explanation: itertools.permutations() generate all ordered arrangements of the string "GeEK". It iterates over the resulting iterator and prints each permutation as a tuple. The default length of permutations is the full length of the string.

Syntax of Itertools.Permutations()

itertools.permutations(iterable, r)

Parameters:

Returns: An iterator that produces tuples, each representing a unique permutation of the elements from the iterable. If r is specified, it generates permutations of length r.

Examples of using Itertools.Permutations()

Example 1: Permutations with mixed data types

Python
from itertools import permutations

print(list(permutations([1, 'geeks'], 2)))
print(list(permutations(range(3), 2)))

Output
[(1, 'geeks'), ('geeks', 1)]
[(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]

Explanation: In the first case, it generates pairs from the list [1, 'geeks'], considering each element's position. In the second case, it generates pairs from the range [0, 1, 2], producing all possible ordered combinations of two elements. The length of each permutation is defined by the second argument (2).

Example 2: Permutations with repeated elements

Python
from itertools import permutations

res = list(permutations([1, 1, 2]))

print(res)

Output
[(1, 1, 2), (1, 2, 1), (1, 1, 2), (1, 2, 1), (2, 1, 1), (2, 1, 1)]

Explanation: Even though there are duplicate elements in the input list, permutations() treats them as unique by position. So it generates all possible ordered arrangements, even if they look the same. Use set() to get only unique permutations.

Example 3: Permutations of a string with specified length

Python
from itertools import permutations

print(list(permutations("ABC", 3)))

Output
[('A', 'B', 'C'), ('A', 'C', 'B'), ('B', 'A', 'C'), ('B', 'C', 'A'), ('C', 'A', 'B'), ('C', 'B', 'A')]

Explanation: This code generates all possible ordered arrangements of length 3 from the string "ABC". Since the string has three unique characters, the function will generate all permutations of length 3.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4