I need to be able to make a list that contains all possible combinations of an inputted list.
For example the list [1,2,3] should return [1 [1,2] [1,3] 2 [2,3] 3 [1,2,3]]
The list doesn’t have to be in any particular order. On this site I’ve found lots of functions using the itertools but those are returning objects when I need just a list.
I need to be able to make a list that contains all possible combinations
Share
Simply use
itertools.combinations. For example:Now
combsholds this value:Yes, it’s slightly different from the sample output you provided, but in that output you weren’t listing all possible combinations.
I’m listing the size of the combination before the actual list for each size, if what you need is simply the combinations (without the size, as it appears in your sample output) then try these other version of the code:
Now
combsholds this value: