-1

I need help to generate all possible subsets of given letters in Python.
For instance:

List=['a','b','c']
Subset=['a','b','c','ab','ac','bc','abc']
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
  • 3
    please post in algorithm tag, it has very less to do with python and see if this helps https://www.geeksforgeeks.org/python-program-to-get-all-subsets-of-given-size-of-a-set/ – ashish singh May 22 '20 at 05:32
  • Does this answer your question? [How to get all possible combinations of a list’s elements?](https://stackoverflow.com/questions/464864/how-to-get-all-possible-combinations-of-a-list-s-elements) – Aaron May 22 '20 at 19:03

1 Answers1

0

From the input and output you have given the following code will work for you,

Code:

list1=['a','b','c']
sublist = list()
for i in range(len(list1)): 
    for j in range(i+1, len(list1)+1):   
        sub = list1[i:j]
        s1=""
        for j in sub:
            s1+=j   
        sublist.append(s1) 

print(sublist)

Output:

['a', 'ab', 'abc', 'b', 'bc', 'c']
  1. The above code does not gives all the subset, you may have to modify it as per your requirement
Prathamesh
  • 1,064
  • 1
  • 6
  • 16