relation between tuple values in a list according to their position
<p>Consider a list containing tuples like:</p>

<pre><code>tuplelist = [('a','b', 'c', 'd'), (6, 3, 9, 11), (0, 4, 5, 6)]
</code></pre>

<p>How can i match corresponding tuple positions.For ex for <code>'d'</code>:</p>

<pre><code>(d, 'has_val', 11)
(d, 'has_val', 6)
</code></pre>

<p>i have tried the following:</p>

<pre><code>str = 'has_val'
for i in tuplelist:
    rel = (i[3],str,i[3])
</code></pre>

<p>but this doesn't give me the desired output</p>

<p>Sounds like <code>zip</code> is your friend:</p>

<pre><code>&gt;&gt;&gt; zip(*tuplelist)
[('a', 6, 0), ('b', 3, 4), ('c', 9, 5), ('d', 11, 6)]
</code></pre>

<p>You can use <code>zip</code> a little bit like a matrix transpose in math.  Anyway, for your situation, we can do:</p>

<pre><code>for x in zip(*tuplelist):
    for y in x[1:]:
        print (x[0], 'has_val', y)
</code></pre>

<p>Gives:</p>

<pre><code>('a', 'has_val', 6)
('a', 'has_val', 0)
('b', 'has_val', 3)
('b', 'has_val', 4)
('c', 'has_val', 9)
('c', 'has_val', 5)
('d', 'has_val', 11)
('d', 'has_val', 6)
</code></pre>

<p>You can do this in a giant one-liner, too:</p>

<pre><code>&gt;&gt;&gt; [(x[0], 'has_val', y) for x in zip(*tuplelist) for y in x[1:]]
[('a', 'has_val', 6),
 ('a', 'has_val', 0),
 ('b', 'has_val', 3),
 ('b', 'has_val', 4),
 ('c', 'has_val', 9),
 ('c', 'has_val', 5),
 ('d', 'has_val', 11),
 ('d', 'has_val', 6)]
</code></pre>

python list python-3.x tuples