Python combination generation
python, python-2.7, python-2.x
Solution
use `itertools.product()` here:
>>> list_1 = ['1','2','3']
>>> list_2 = ['2','5','7']
>>> list_3 = ['9','9','8']
>>> from itertools import product
>>> ["".join(x) for x in product(list_1,list_2,list_3)]
['129', '129', '128', '159', '159', '158', '179', '179', '178', '229', '229', '228', '259', '259', '258', '279', '279', '278', '329', '329', '328', '359', '359', '358', '379', '379', '378']
Problem
I am new to programming and to Python. Not sure how to proceed to achieve this (explained below) problem, hence the question. I have n number of lists, each containing 1 or more items. I want to have a new list with all possible combinations, which uses one item from each list once, and always. Example: ``` list_1 = ['1','2','3'] list_2 = ['2','5','7'] list_3 = ['9','9','8'] ``` Result would be: `['129', '129', '128', '159', '159', '158', '179', '179', '178', '229', '229', '228', '259', '259', '258', '329', '329', '328', '359', '359','358', '379', '379', '378']` Example here has 3 lists each with 3 items but there can be any n number of lists each containing any m number of elements (so not all lists need to have same number of elements). All elements of lists are strings and output list also contains strings. What should I do? I looked at itertools.combinations but I have no idea as to how to employ it for this task.