-2

Here is what I am trying to do but not exactly sure how to make this work. I have 3 sets of variables and I want to output all combinations of those 3 sets and output in a format that maintains the order of the variables like this:

list_of_vars = [var1, var2, var3]

The variables would look something like this:

var1 = [1, 2, 3]
var2 = ["foo", "bar", "foo2"]
var3 = ["a", "b", "c"]

The final output should look like this.

final_list_of_vars = [[1, "foo", a], [1, "bar", a], .......]
MixedBeans
  • 159
  • 4
  • 17

1 Answers1

0

We just need to iterate through those variables and make a new list with them.

Here is the sample code:

list1 = [1,2,3,4]
list2 = ['a','b','c','d','e']
list3 = ["Hello", "World","Python"]
answer = []
for i in list1:
    for j in list2:
        for k in list3:
            answer.append([i,j,k])
print(answer)

Here's the sample result:

[[1, 'a', 'Hello'], [1, 'a', 'World'], [1, 'a', 'Python'], [1, 'b', 'Hello'], [1, 'b', 'World'], [1, 'b', 'Python'], [1, 'c', 'Hello'], [1, 'c', 'World'], [1, 'c', 'Python'], [1, 'd', 'Hello'], [1, 'd', 'World'], [1, 'd', 'Python'], [1, 'e', 'Hello'], [1, 'e', 'World'], [1, 'e', 'Python'], [2, 'a', 'Hello'], [2, 'a', 'World'], [2, 'a', 'Python'], [2, 'b', 'Hello'], [2, 'b', 'World'], [2, 'b', 'Python'], [2, 'c', 'Hello'], [2, 'c', 'World'], [2, 'c', 'Python'], [2, 'd', 'Hello'], [2, 'd', 'World'], [2, 'd', 'Python'], [2, 'e', 'Hello'], [2, 'e', 'World'], [2, 'e', 'Python'], [3, 'a', 'Hello'], [3, 'a', 'World'], [3, 'a', 'Python'], [3, 'b', 'Hello'], [3, 'b', 'World'], [3, 'b', 'Python'], [3, 'c', 'Hello'], [3, 'c', 'World'], [3, 'c', 'Python'], [3, 'd', 'Hello'], [3, 'd', 'World'], [3, 'd', 'Python'], [3, 'e', 'Hello'], [3, 'e', 'World'], [3, 'e', 'Python'], [4, 'a', 'Hello'], [4, 'a', 'World'], [4, 'a', 'Python'], [4, 'b', 'Hello'], [4, 'b', 'World'], [4, 'b', 'Python'], [4, 'c', 'Hello'], [4, 'c', 'World'], [4, 'c', 'Python'], [4, 'd', 'Hello'], [4, 'd', 'World'], [4, 'd', 'Python'], [4, 'e', 'Hello'], [4, 'e', 'World'], [4, 'e', 'Python']]