How to Get All Possible Combinations of a List'S Elements

All possible combinations of elements in a list which are next to each other

You can use a recursive generator function:

stuff = [1, 2, 3, 4]
def get_combos(d, c = []):
yield tuple(c)
if d:
yield from get_combos(d[1:], c+[d[0]])
yield from get_combos(d[1:], [d[0]])

result = set(filter(None, get_combos(stuff)))


(1, 2),
(2, 3),
(3, 4),
(1, 2, 3),
(2, 3, 4),
(1, 2, 3, 4)}

How to generate all the possible combinations for a list of elements grouped in different sizes?

You should take a look at the more-itertools library. And more particularly to the set_partitions method that should suits your needs.

from more-itertools import set_partitions

fruits = ['Apple', 'Orange', 'Banana', 'Watermelon']

for itm in set_partitions(fruits)):

The ouput produce is :

[['Apple', 'Orange', 'Banana', 'Watermelon']]
[['Apple'], ['Orange', 'Banana', 'Watermelon']]
[['Apple', 'Orange'], ['Banana', 'Watermelon']]
[['Orange'], ['Apple', 'Banana', 'Watermelon']]
[['Apple', 'Orange', 'Banana'], ['Watermelon']]
[['Orange', 'Banana'], ['Apple', 'Watermelon']]
[['Apple', 'Banana'], ['Orange', 'Watermelon']]
[['Banana'], ['Apple', 'Orange', 'Watermelon']]
[['Apple'], ['Orange'], ['Banana', 'Watermelon']]
[['Apple'], ['Orange', 'Banana'], ['Watermelon']]
[['Apple'], ['Banana'], ['Orange', 'Watermelon']]
[['Apple', 'Orange'], ['Banana'], ['Watermelon']]
[['Orange'], ['Apple', 'Banana'], ['Watermelon']]
[['Orange'], ['Banana'], ['Apple', 'Watermelon']]
[['Apple'], ['Orange'], ['Banana'], ['Watermelon']]

And if you want all every combinations possible if you reorder your orignal list you can use the previous method in combination with the the permutations method of itertools with something like:

import itertools
from more_itertools import set_partitions

fruits = ['Apple', 'Orange', 'Banana', 'Watermelon']
output = list()

for x in itertools.permutations(fruits):
output += list(set_partitions(x))

How to generate all possible combinations from the letters of strings in a list

I can propose you a trivial algorithm to do the same, it is using recursion:

def associations(entry):
while len(entry) > 2:
to_treat_later = entry.pop()
print(f"We will treat {to_treat_later} later")
entry = associations(entry)
print(f"we can start with {entry}")
associated_entry = []
for elt_1 in entry[0]:
for elt_2 in entry[1]:
return [associated_entry]

def convert_entry(entry):
converted_entry = []
for elt in entry:
list_elt = []
for letter in elt:
return converted_entry

the_entry = ["jkl", "ghi", "def", "cfe"]

How to generate all possible combinations from list elements in Python having Pandas DataFrames in list?

Are you sure? product() iterates over the iterables passed to, it but only one level deep.

>>> from itertools import product
>>> mylist = [[1, 2], ['a', 'b'], [[4, 6], [8, 9]]]
>>> for x in product(*mylist):
... print(x)
(1, 'a', [4, 6])
(1, 'a', [8, 9])
(1, 'b', [4, 6])
(1, 'b', [8, 9])
(2, 'a', [4, 6])
(2, 'a', [8, 9])
(2, 'b', [4, 6])
(2, 'b', [8, 9])

See? That [4, 6] and [8, 9] are themselves iterables is irrelevant to product().

Related Topics

Leave a reply