pythonlisttupleslist-manipulation

How to combine two lists to get the following desired result containing tuples?


I have two lists as:

a = ['apple', 'mango', 'pear']

b = ['ripe','raw','rotten']

How can I get the following result list of tuples as:

[(('apple', 'mango', 'pear'), 'ripe'), (('apple', 'mango', 'pear'), 'raw'), (('apple', 'mango', 'pear'), 'rotten')]

Solution

  • list(itertools.product(a,b)) will use the element in the A.To make the full list as a element, you could use nested list,like:

    list(itertools.product([tuple(a)], b)
    

    Result:

    [(('apple', 'mango', 'pear'), 'ripe'), (('apple', 'mango', 'pear'), 'raw'), (('apple', 'mango', 'pear'), 'rotten')]