Expanding a path of lists

I’m struggling with a good way to do this and wonder if there is a tried and tested method or a module that has this kind of capability? What I want to do is take something of the form:

/[Part1a, Part1b]/[Part2a, Part2b, Part2c]/[Part3a, Part3b]/

and expand it to all possible combinations, i.e.

/Part1a/Part2a/Part3a
/Part1a/Part2a/Part3b
/Part1a/Part2b/Part3a
/Part1a/Part2b/Part3b
/Part1a/Part2c/Part3a
/Part1a/Part2c/Part3b

/Part2a/Part2a/Part3a
/Part2a/Part2a/Part3b
/Part2a/Part2b/Part3a
/Part2a/Part2b/Part3b
/Part2a/Part2c/Part3a
/Part2a/Part2c/Part3b

The number of list members and path elements can vary. Any pointers?

1 Like

If only I’d studied a bit more maths when I was younger I would have known that what I was looking for was the “product” of these lists!

So I’ve found “itertools.product”

import itertools

parts = [
    ['Part1a', 'Part1b'],
    ['Part2a', 'Part2b', 'Part2c'],
    ['Part3a', 'Part3b']
]

list(itertools.product(*parts))
 

I love Python :slight_smile:

2 Likes