1

I have a lists of lists, like this:

indices = [[0, 1, 2], [0, 1], [0], [0, 1, 2], [0, 1]]

Now I want to loop over all of these indices, meaning that I want to go for the combinations [0, 0, 0, 0, 0], [0, 0, 0, 0, 1], [0, 0, 0, 1, 0], ..., in effect the same as

for i in indices[0]:
  for j in indices[1]:
    for k in indices[2]:
      ....

I know that this will lead to a combinatorical explosion pretty fast, but I know that everything I'll need this for is tractable.

I am simply looking for a general way to write that, and I think one could build a (recursive?) generator for this, but I cannot wrap my head around how that must look like.

If I knew all of the sub-lists had a fixed length, I would generate the product of the lens of the sublist and then get the binary/ternary/decimal/... representation of that number, however both the length of the sub-lists as well as the length of indices is variable.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Chris Stenkamp
  • 337
  • 1
  • 2
  • 15

0 Answers0