Python: What is the most efficient way to create list of lists that each item is unique depending on another nested list?

Question:

I have REST API that receive arbitrary data which I sort and convert it into list of lists, e.g.

attributes = [ ['nike', 'adidas', 'salmon'], ['38'], ['blue', 'grey'] ]

What I want is to create new list of lists that each element in sub-lists mapped uniquely with others, like:

paths = [ ['nike', '38', 'blue'], 
          ['nike', '38', 'grey'],
          ['adidas', '38', 'blue'], 
          ['adidas', '38', 'grey'],
          ['salmon', '38', 'blue'], 
          ['salmon', '38', 'grey'] ]

I appreciate your answers and suggestions to achieve that in most possible efficient way.

Asked By: Wadhah Sky

||

Answers:

As comments suggest, you can use product function from itertools lib.

>>> from itertools import product
>>> attributes = [ ['nike', 'adidas', 'salmon'], ['38'], ['blue', 'grey'] ]
>>> product(*attributes)
<itertools.product object at 0x7fba3ca3a080>
>>> set(product(*attributes))
{('adidas', '38', 'grey'), ('salmon', '38', 'grey'), ('nike', '38', 'blue'), ('adidas', '38', 'blue'), ('salmon', '38', 'blue'), ('nike', '38', 'grey')}

Answered By: Amir reza Riahi