I recently learned the concepts of permutation and combination in my Statistics course and I was thinking if I can find all permutations and all combinations of a data structure in Python. For example, if I have a list
or an array
, is it possible to find all permutations and combinations of it? And if I have a list
like:
lst = [1, 2, 3, 4]
Output for permutation taken 3 elements at a time:
[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]
Does Python provide flexibility if I want to find all permutations of 2 or all combinations of 3 for a list
containing more than 2 elements? If there are methods for doing this, please provide them below using example codes.