In Python, I am trying to add all of the different combinations of the elements in a list of N
lists, where N
is a variable. Specifically, I'm working with a list containing N copies of the list [1, 2, 3, 4, 5, 6]
.
So let's assume N = 3
. I want to cycle through the first list, adding:
6 + 6 + 6
5 + 6 + 6
...
1 + 6 + 6`
then increment the second guy once and start adding:
6 + 5 + 6
5 + 5 + 6
...
1 + 1 + 1 # ultimately to
after of course I increment the third guy. This specific example can be done with 3 nested for loops, but of course, I need the number of loops to change. I search around the web and this forum for answers and wasn't quite able to grasp what I should do. People have been saying to use recursive functions, but I've never even heard of that until I looked this up. So it's kind of hard to grasp.