Itertools in python

I have a list a = ‘123’
I want it to make it like that [('1'),('2'),('3'),('1','2'),('1','3'),('2','3'),('1','2','3')]
Is there any way to make it like that using itertools or more efficient way?

You may be looking for itertools.combinations()

1 Like

Hello!

Maybe and hopefully you have already the solution! If not try to follow the way described below the find a possible solution.

I hope it works and good luck!

  1. import itertools to use combinations and chain.from_iterable.
  2. Define a function with a string as argument.
  3. Create a list of that string.
  4. Declare an empty list to store the values of the combinations of the list.
  5. Use a for-loop in the range from 1 to the 1 + (length of the created list)
    to calculate all possible combinations and append their values in in the
    created empty list.
  6. Use “chain.from_iterable” on the list under 4) and assign that result to a
    variable, that you have to declare.
  7. apply “print” on the value obtained under 5).
1 Like

I am very sorry!

6. Use “chain.from_iterable” on the list under 5. and not under 4).
7. Apply “print”…under 6. and not under 5).

1 Like