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

# 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)))``

Output:

``{(1,),  (2,),  (3,),  (4,),  (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_partitionsfruits = ['Apple', 'Orange', 'Banana', 'Watermelon']for itm in set_partitions(fruits)):    print(itm)``

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']]``
EDIT:

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 itertoolsfrom more_itertools import set_partitionsfruits = ['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)        entry.append(to_treat_later)    else:        print(f"we can start with {entry}")        associated_entry = []        for elt_1 in entry[0]:            for elt_2 in entry[1]:                associated_entry.append(f"{elt_1}{elt_2}")        return [associated_entry]def convert_entry(entry):    converted_entry = []    for elt in entry:        list_elt = []        for letter in elt:            list_elt.append(letter)        converted_entry.append(list_elt)    return converted_entrythe_entry = ["jkl", "ghi", "def", "cfe"]associations(convert_entry(the_entry))``

## 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()`.