Python itertools.permutations Function

📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.

🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.

▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube

▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube

The itertools.permutations function in Python's itertools module returns successive r-length permutations of elements from the input iterable. This is useful for generating all possible arrangements of a set of elements.

Table of Contents

  1. Introduction
  2. itertools.permutations Function Syntax
  3. Examples
    • Basic Usage
    • Specifying the Length of Permutations
    • Permutations of Characters in a String
    • Permutations of a Subset
  4. Real-World Use Case
  5. Conclusion

Introduction

The itertools.permutations function creates an iterator that produces successive r-length permutations of elements from the input iterable. If the length is not specified, it generates all possible permutations of the iterable.

itertools.permutations Function Syntax

Here is how you use the itertools.permutations function:

import itertools

iterator = itertools.permutations(iterable, r=None)

Parameters:

  • iterable: The input iterable from which permutations are generated.
  • r: Optional. The length of each permutation. If not specified, it defaults to the length of the iterable.

Returns:

  • An iterator that yields tuples of permutations.

Examples

Basic Usage

Generate all permutations of a list.

Example

import itertools

data = [1, 2, 3]
result = itertools.permutations(data)

print(list(result))

Output:

[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]

Specifying the Length of Permutations

Generate permutations of a specified length.

Example

import itertools

data = [1, 2, 3]
result = itertools.permutations(data, 2)

print(list(result))

Output:

[(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]

Permutations of Characters in a String

Generate all permutations of characters in a string.

Example

import itertools

data = 'ABC'
result = itertools.permutations(data)

print([''.join(p) for p in result])

Output:

['ABC', 'ACB', 'BAC', 'BCA', 'CAB', 'CBA']

Permutations of a Subset

Generate permutations of a subset of elements.

Example

import itertools

data = [1, 2, 3, 4]
result = itertools.permutations(data, 3)

print(list(result))

Output:

[(1, 2, 3), (1, 2, 4), (1, 3, 2), (1, 3, 4), (1, 4, 2), (1, 4, 3), (2, 1, 3), (2, 1, 4), (2, 3, 1), (2, 3, 4), (2, 4, 1), (2, 4, 3), (3, 1, 2), (3, 1, 4), (3, 2, 1), (3, 2, 4), (3, 4, 1), (3, 4, 2), (4, 1, 2), (4, 1, 3), (4, 2, 1), (4, 2, 3), (4, 3, 1), (4, 3, 2)]

Real-World Use Case

Generating Password Combinations

Use permutations to generate all possible password combinations from a set of characters.

Example

import itertools

characters = 'abc'
password_length = 2
possible_passwords = itertools.permutations(characters, password_length)

print([''.join(p) for p in possible_passwords])

Output:

['ab', 'ac', 'ba', 'bc', 'ca', 'cb']

Conclusion

The itertools.permutations function is used for generating all possible arrangements of a set of elements. It provides flexibility in specifying the length of permutations and can be used in various applications, such as generating password combinations or solving combinatorial problems.

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare