iItertools.product with variable validation
<p>I have this form of list</p>

<pre><code>data = [
   [
    {'name': 's11', 'class': 'c1'}, {'name': 's12', 'class': 'c2'}
   ],
   [
    {'name': 's21', 'class': 'c2'}, {'name': 's22', 'class': 'c2'}
   ],
   [
    {'name': 's31', 'class': 'c1'}, {'name': 's32', 'class': 'c1'}
   ]
]
</code></pre>

<p>by using the <code>itertools.product(data)</code>
I receive all the possible combinations needed by taking one element from each list in the main list data.
What I want to do, I want to skip if the element in the first sublist have a different class in the second or third sublist.</p>

<p>Does itertools.product provide any validation options for such case ?</p>

<p>The expected results should be:</p>

<pre><code>({'name': 's11', 'class': 'c1'},{'name': 's31', 'class': 'c1'}),
( {'name': 's11', 'class': 'c1'}, {'name': 's32', 'class': 'c1'}),
({'name': 's12', 'class': 'c2'},{'name': 's21', 'class': 'c2'}),
({'name': 's12', 'class': 'c2'},{'name': 's22', 'class': 'c2'}),
</code></pre>

<pre><code>from itertools import chain, combinations, product
result = [
    (a, b) for a, b 
    in chain.from_iterable(product(*l) for l in combinations(data, 2)) 
    if a['class'] == b['class']
]
</code></pre>

python list dictionary itertools