To tackle the problem of the comma separation we can split each string in the list.
data = ['a, b', 'c', 'd, e']
print([value.split(',') for value in data])
This gives us ['a', ' b'], ['c'], ['d', ' e']]
To get rid of those extra whitespaces we can modify the function by using map
to apply str.strip
to each resulting element of the inner list.
data = ['a, b', 'c', 'd, e']
print([list(map(str.strip, value.split(','))) for value in data])
The result is [['a', 'b'], ['c'], ['d', 'e']]
Now we can use itertools.product
for the final combinations.
import itertools
data = ['a, b', 'c', 'd, e']
data_normalized = [list(map(str.strip, value.split(','))) for value in data]
print(list(itertools.product(*data_normalized)))
The result is [('a', 'c', 'd'), ('a', 'c', 'e'), ('b', 'c', 'd'), ('b', 'c', 'e')]
.