🎓 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 (178K+ 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
- Introduction
itertools.permutationsFunction Syntax- Examples
- Basic Usage
- Specifying the Length of Permutations
- Permutations of Characters in a String
- Permutations of a Subset
- Real-World Use Case
- 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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment