How to Generate Permutations of a List

79 views

How to Generate Permutations of a List

How to Generate Permutations of a List

I need to generate all possible permutations of a list. How can I do this?

solveurit24@gmail.com Changed status to publish February 16, 2025
0

Python’s itertools module provides a convenient way to generate permutations.

Code Example:

import itertools

def generate_permutations(lst):
    permutations = itertools.permutations(lst)
    return [list(p) for p in permutations]

# Example usage
sample_list = [1, 2, 3]
print(generate_permutations(sample_list))

This function returns a list of all permutations of the input list using itertools.permutations.

solveurit24@gmail.com Changed status to publish February 16, 2025
0
You are viewing 1 out of 1 answers, click here to view all answers.