# Combination of a list with permutations of another list

## Question:

I have two lists:

``````object = ['square','circle','triangle']
description = ['red','green']
``````

I want as output a list of dictionaries:

``````{'square': 'red', 'circle': 'red', 'triangle': 'red'}
{'square': 'red', 'circle': 'red', 'triangle': 'green'}
{'square': 'green', 'circle': 'red', 'triangle': 'green'}
``````

etc.

So the objects don’t repeat, but the descriptions may. Each dictionary has as its keys the original list of objects.

I’m not sure of the name of the algorithm I want, so I’m having trouble finding the correct one. (I’ve looked at permutations of two lists in python, but that’s seeking a different result. Permutations of two lists is the same question, but using OCAML.)

You could use `itertools.product` to generate all possible combinations of your `descriptions`.

Then create a dictionary with the `objects` as keys and the “combination” as values:

``````>>> import itertools

>>> objects = ['square','circle','triangle']
>>> description = ['red','green']

>>> [dict(zip(objects, comb)) for comb in itertools.product(description, repeat=len(objects))]
[{'circle': 'red', 'square': 'red', 'triangle': 'red'},
{'circle': 'red', 'square': 'red', 'triangle': 'green'},
{'circle': 'green', 'square': 'red', 'triangle': 'red'},
{'circle': 'green', 'square': 'red', 'triangle': 'green'},
{'circle': 'red', 'square': 'green', 'triangle': 'red'},
{'circle': 'red', 'square': 'green', 'triangle': 'green'},
{'circle': 'green', 'square': 'green', 'triangle': 'red'},
{'circle': 'green', 'square': 'green', 'triangle': 'green'}]
``````
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.