all possible combinations of items in n lists


Bernat Ibanez

I need to develop a list that contains all possible combinations in order of elements in n lists. Basically, I'm trying to find all possible paths that will be needed later in another part of the program.

I've written some simple code for the two lists, but the problem is that I don't know how much input the user will give, so I have to guess. Currently, I have defined a function that outputs all possible combinations (only one way, since they are paths). I've also been testing other alternatives like itertools (an answer that I think might solve my problem), or using numpy arrays (the problem with this is that my arrays are not homogeneous).

The input list might look like this (3 dimensions):

chords = [[[1, 4, 8, 12], [1, 4, 10, 12]], [[4, 7, 13, 19], [4, 9, 13, 21]]]

My function may generate permutations between two lists:

def combination(list1, list2):
    list = []
    for x in list1:
        for y in list2:
            list.append([x,y])
    return list

combination(chords[0], chords[1])

This function is there for the purpose, but the problem is that, for example, when I introduce combination(combination(chords[0], chords[1]), chords[3])it, this is not calculated separately chords[0]and chords[1](of course, it works as expected).

edit:

Ok, as @iBug pointed out, a good way is to use itertools.product():

bases_chords = [···] #It's a three dimensional array I've filled out  before
possibilities = [] #The list that will contain all the different combinations

for a in product(*bases_chords): #The asterisk means that I input everything on the list
    possibilities.append(a)

print(possibilities)
print(len(possibilities)) #Just to check if the dimensions are right
iBug

itertools.productis what you are looking for. It takes multiple Iterables (lists are iterable) and produces a generator that loops through all combinations for each combination.

See example:

>>> for a, b, c in itertools.product([1, 2, 3], "abc", [True, False]):
...  print(a, b, c)
...
1 a True
1 a False
1 b True
1 b False
1 c True
1 c False
2 a True
2 a False
2 b True
2 b False
2 c True
2 c False
3 a True
3 a False
3 b True
3 b False
3 c True
3 c False
>>>

So your use case would become:

itertools.product(*chords)

Related


all possible combinations of items in n lists

Bernat Ibanez I need to develop a list that contains all possible combinations in order of elements in n lists. Basically, I'm trying to find all possible paths that will be needed later in another part of the program. I've written some simple code for the two

all possible combinations of k with lists of size n

Theodore Narliyski: I want to get all possible combinations of size K from a list of size N. I have a list with "person" objects and I am trying to create a new ArrayList which will be filled with the list of objects. Each table will be a different combination

all possible combinations of k with lists of size n

Theodore Narliyski: I want to get all possible combinations of size K from a list of size N. I have a list with "person" objects and I am trying to create a new ArrayList which will be filled with the list of objects. Each table will be a different combination

make all possible combinations of lists

Dean: I need to be able to create a list containing all possible combinations of input lists. For example, a list [1,2,3]should return [1 [1,2] [1,3] 2 [2,3] 3 [1,2,3]]the list does not have to be in any particular order. On this site I found many functions th

all possible combinations of vector lists

citizen I have a list of vectors and I want to get a list of all possible combinations between the elements of each vector, i.e. combinations of n elements (from a vector) taken two or more at a time. For example, I have the following list: > DF $`1` A B

all possible combinations of two lists

wei Given I have two lists: val ints = listOf(0, 1, 2) val strings = listOf("a", "b", "c") I want all possible combinations of their elements 0a, 1a, 2a, 0betc Is there a more elegant way than: ints.forEach { int -> strings.forEach { string ->

make all possible combinations of lists

Dean: I need to be able to create a list containing all possible combinations of input lists. For example, a list [1,2,3]should return [1 [1,2] [1,3] 2 [2,3] 3 [1,2,3]]the list does not have to be in any particular order. On this site I found many functions th

all possible combinations of two lists

wei Given I have two lists: val ints = listOf(0, 1, 2) val strings = listOf("a", "b", "c") I want all possible combinations of their elements 0a, 1a, 2a, 0betc Is there a more elegant way than: ints.forEach { int -> strings.forEach { string ->

make all possible combinations of lists

Dean: I need to be able to create a list containing all possible combinations of input lists. For example, a list [1,2,3]should return [1 [1,2] [1,3] 2 [2,3] 3 [1,2,3]]the list does not have to be in any particular order. On this site I found many functions th

all possible combinations of vector lists

citizen I have a list of vectors and I want to get a list of all possible combinations between the elements of each vector, i.e. combinations of n elements (from a vector) taken two or more at a time. For example, I have the following list: > DF $`1` A B

all possible combinations of two lists

wei Given I have two lists: val ints = listOf(0, 1, 2) val strings = listOf("a", "b", "c") I want all possible combinations of their elements 0a, 1a, 2a, 0band many more Is there a more elegant way than: ints.forEach { int -> strings.forEach { string ->

all possible combinations of two lists

wei Given I have two lists: val ints = listOf(0, 1, 2) val strings = listOf("a", "b", "c") I want all possible combinations of their elements 0a, 1a, 2a, 0band many more Is there a more elegant way than: ints.forEach { int -> strings.forEach { string ->

How to form all possible combinations from n lists

K split X This question is hard to put in a title, so an example is needed: I have 3 lists: L1 = ["Eagle", "Panther"] L2 = ["Warrior", "Talon", "Machete"] L3 = ["Feather", "Raptor", "Hunter", "Piranha"] The size of the list can vary. I want to form all subs

How to form all possible combinations from n lists

K split X This question is hard to put in a title, so an example is needed: I have 3 lists: L1 = ["Eagle", "Panther"] L2 = ["Warrior", "Talon", "Machete"] L3 = ["Feather", "Raptor", "Hunter", "Piranha"] The size of the list can vary. I want to form all subs

How to form all possible combinations from n lists

K split X This question is hard to put in a title, so an example is needed: I have 3 lists: L1 = ["Eagle", "Panther"] L2 = ["Warrior", "Talon", "Machete"] L3 = ["Feather", "Raptor", "Hunter", "Piranha"] The size of the list can vary. I want to form all subs

How to form all possible combinations from n lists

K split X This question is hard to put in a title, so an example is needed: I have 3 lists: L1 = ["Eagle", "Panther"] L2 = ["Warrior", "Talon", "Machete"] L3 = ["Feather", "Raptor", "Hunter", "Piranha"] The size of the list can vary. I want to form all subs

How to form all possible combinations from n lists

K split X This question is hard to put in a title, so an example is needed: I have 3 lists: L1 = ["Eagle", "Panther"] L2 = ["Warrior", "Talon", "Machete"] L3 = ["Feather", "Raptor", "Hunter", "Piranha"] The size of the list can vary. I want to form all subs

intersection of all combinations of n lists

high tech physics Given an ArrayList of ArrayLists of size greater than 3 ArrayList<ArrayList<Integer>> lists = new ArrayLists<ArrayList<Integer>>(); I want to take 3 unique sublists, find their intersection, and repeat this process for all possible combinati

Get all combinations of N items

Warlax56 I have a list of items: [0,1,10,20,5,6,7] Is there an easy, Pythonic way to get all groupings of n variables? In this case, similar groups with different orders are considered duplicates. 3: (0,1,10) (0,1,20) (0,2,5) ... 4: (0,1,10,20) (0,1,10,5) (0

Get all combinations of N items

Warlax56 I have a list of items: [0,1,10,20,5,6,7] Is there an easy, Pythonic way to get all groupings of n variables? In this case, similar groups with different orders are considered duplicates. 3: (0,1,10) (0,1,20) (0,2,5) ... 4: (0,1,10,20) (0,1,10,5) (0

Get all combinations of N items

Warlax56 I have a list of items: [0,1,10,20,5,6,7] Is there an easy, Pythonic way to get all groupings of n variables? In this case, similar groups with different orders are considered duplicates. 3: (0,1,10) (0,1,20) (0,2,5) ... 4: (0,1,10,20) (0,1,10,5) (0

Get all combinations of N items

Warlax56 I have a list of items: [0,1,10,20,5,6,7] Is there an easy, Pythonic way to get all groupings of n variables? In this case, similar groups with different orders are considered duplicates. 3: (0,1,10) (0,1,20) (0,2,5) ... 4: (0,1,10,20) (0,1,10,5) (0

Get all combinations of N items

Warlax56 I have a list of items: [0,1,10,20,5,6,7] Is there an easy, Pythonic way to get all groupings of n variables? In this case, similar groups with different orders are considered duplicates. 3: (0,1,10) (0,1,20) (0,2,5) ... 4: (0,1,10,20) (0,1,10,5) (0