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>Firstly, Don't name your variable as <code>str</code> as it shadows the builtin function. </p>

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

<p>Now coming to your issue, It's aesthetic to have the first row out as a header row. Also note that, I've used the <code>index</code> function to find the index instead of directly hard coding <code>3</code>. This is helpful if there is a change in the number of columns. </p>

<pre><code>header_row = tuplelist[0]
column_name = 'd'
column_index = header_row.index(column_name)
</code></pre>

<p>Now for the logic, Loop from the second element using slices. </p>

<pre><code>for i in tuplelist[1:]:
    print(column_name,s,i[column_index])
</code></pre>

<p>This would give you the desired output. </p>

python list python-3.x tuples