I have a list of lists and a separator string like this:
lists = [
['a', 'b'],
[1, 2],
['i', 'ii'],
]
separator = '-'
As result I want to have a list of strings combined with separator string from the strings in the sub lists:
result = [
'a-1-i',
'a-1-ii',
'a-2-i',
'a-2-ii',
'b-1-i',
'b-1-ii',
'b-2-i',
'b-2-ii',
]
Order in result is irrelevant.
How can I do this?
itertools.productreturns an iterator that produces the cartesian product of the provided iterables. We need tomapstrover the resultant tuples, since some of the values are ints. Finally, we can join the stringified tuples and throw the whole thing inside a list comprehension (or generator expression if dealing with a large dataset, and you just need it for iteration).