python combinations and permutations code example

Example 1: python permutation

import itertools

a = [1, 2, 3]
n = 3

perm_iterator = itertools.permutations(a, n)

for item in perm_iterator:
    print(item)

Example 2: how to get all possible combinations in python

all_combinations = [list(zip(each_permutation, list2)) for each_permutation in itertools.permutations(list1, len(list2))]

Example 3: combinations and permutations in python

from itertools import permutations
from itertools import combinations

Example 4: permutation and combination code in python

# A Python program to print all  
# permutations of given length  
from itertools import permutations  
  
# Get all permutations of length 2  
# and length 2  
perm = permutations([1, 2, 3], 2)  
  
# Print the obtained permutations  
for i in list(perm):  
    print (i)