2

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

I've already made some simple code for two lists, but the problem is that I don't know how many inputs will the user give, so I have to guess. For the moment I've defined a function that outputs all possible combinations (1 way only, because they're paths). I've been also testing other alternatives like itertools (which I think may hold the answer to my problem), or using numpy arrays (the problem with this is that my array isn't homogeneous).

The input list may look something like this (3 dimensions):

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

My function that could generate the 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 works as intended, but the problem is for example when I introduce combination(combination(chords[0], chords[1]), chords[3]), which doesn't count separately chords[0] and chords[1] (still, it works as intended).

Edit:

Okay, so like @iBug pointed out, a good way to do it is with 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
4
  • 3
    Take a look at itertools.combinations(). Commented May 14, 2019 at 16:14
  • 1
    Can you add the expected output for your chords? It's a bit confusing. Commented May 14, 2019 at 16:16
  • Well, basically it's like this: A chord looks like [1, 4, 8, 12] (they're four notes). Then there's a list that contains all the possible notes for the same base. After that, all the different chords with different bases are in the same list, the list chords. Commented May 14, 2019 at 16:30
  • Congrats for asking your first question and accepting an answer! Don't forget to come back to Stack Overflow if you have other questions. Commented May 14, 2019 at 16:36

1 Answer 1

2

itertools.product is what you're looking for. It takes multiple Iterables (lists are iterables) and produces a generator that loops over all combinations for each of them.

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 turn into:

itertools.product(*chords)
Sign up to request clarification or add additional context in comments.

2 Comments

Nice, seems like what I was looking for!! Going to try it out and confirm if it works. Thanks
I've just tried and it works like a charm!!! Gonna edit my original post to show the final answer that worked for me. Thanks! I'm gonna check the tick mark

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.