All possible subsets in Python

Problem Statement:

Here, we are given a sentence and we need to find and print all possible subsets from the words of the sentence. In simple words, we need to find permutations of the words in the sentence.

For example,
Input
s = ‘I love CopyAssignment’
Output

I love CopyAssignment
I CopyAssignment love
love I CopyAssignment
love CopyAssignment I
CopyAssignment I love
CopyAssignment love I

Code to find all possible subsets in Python:

import itertools
 
s = 'I love CopyAssignment'
s = s.split()
permutations = list(itertools.permutations(s))
 
s = [' '.join(permutation) for permutation in permutations]
for i in s:
    print(i)

Output:

Output for all possible subsets in Python

Also Read:

Share:

Author: Harry

Hello friends, thanks for visiting my website. I am a Python programmer. I, with some other members, write blogs on this website based on Python and Programming. We are still in the growing phase that's why the website design is not so good and there are many other things that need to be corrected in this website but I hope all these things will happen someday. But, till then we will not stop ourselves from uploading more amazing articles. If you want to join us or have any queries, you can mail me at admin@copyassignment.com Thank you