Managing multiple lists with multiple tuples
<p>I have approximately 2000 lines of data in the following format:</p>

<pre><code>.
.
[(0, 1, 'Blank', ''), (0, 3, 'NAME', 'Ayiesha Woods'), (5, 10, 'DOB', 'July 2 , 1979'), (10, 13, 'LOC', 'Long Island'), (13, 16, 'LOC', 'New York')]
[(0, 1, 'Blank', ''), (0, 3, 'NAME', 'Craig Rivera'), (7, 12, 'DOB', 'October 10 , 1954'), (5, 7, 'LOC', 'Manhattan')]
[(0, 1, 'Blank', ''), (0, 4, 'NAME', 'Margery Pitt Durant'), (14, 16, 'LOC', 'Flint'), (6, 11, 'DOB', 'May 24 , 1887'), (16, 18, 'LOC', 'Michigan')]
[(0, 1, 'Blank', ''), (0, 3, 'NAME', 'Austin Watson'), (10, 13, 'LOC', 'Ann Arbor'), (13, 15, 'LOC', 'Michigan'), (4, 9, 'DOB', 'January 13 , 1992')]
[(0, 1, 'Blank', ''), (0, 3, 'NAME', 'Gary Spatz'), (5, 8, 'LOC', 'New York'), (16, 19, 'LOC', 'New York'), (19, 21, 'LOC', 'Miami'), (21, 23, 'LOC', 'Florida'), (8, 13, 'DOB', 'April 1 , 1951')]
.
.
.
</code></pre>

<p>They are basically many lists each of them containing details of people like name, dob, loc etc. inside respective tuples.</p>

<p>I want to extract name of all the people and their corresponding dob in the following format:</p>

<pre><code>('Ayiesha Woods', 'DateOfBirth', 'July 2 , 1979')
('Craig Rivera', 'DateOfBirth', 'October 10 , 1954')
</code></pre>

<p>and so on..</p>

<p>This is my attempt:</p>

<pre><code>temp = "DateOFBirth"
results = []

for n1 in text:
    for n2 in text:
        if n1 is not n2:
            if text[1][2] == 'NAME' and text[2][2] == 'DOB':
                rel = text[1][3], temp, text[2][3]
                print(rel)
            results.append(rel)
</code></pre>

<p>This will only output if the name tuple is at position 1 and date tuple is at position 2 in the list which is not always the case.</p>

<p>What do I do if I want to output the result irrespective of the position of the name tuple or date tuple in the list.</p>

<p>EDIT:</p>

<p>I have a list containing tuples like:</p>

<pre><code>text = [(0, 1, 'Blank', ''), (0, 3, 'NAME', 'Vance Trimble'), (5, 7, 'LOC', 'Harrison'), (7, 9, 'LOC', 'Arkansas'), (9, 14, 'DOB', 'July 6 , 1913')]
</code></pre>

<p>I am looking to extract data in the following format:</p>

<pre><code>('Vance Trimble', 'DateOFBirth', 'July 6 , 1913')
</code></pre>

<p>My code:</p>

<pre><code>temp = "DateOFBirth"
if text[1][2] == 'NAME' and text[4][2] == 'DOB':
    rel = text[1][3], temp, text[4][3]
    print(rel)
</code></pre>

<p>Ho do i do this without having to hardcode like:</p>

<pre><code>text[1][2] == 'NAME' and text[4][2] == 'DOB'
</code></pre>

<p>so that it searches the list by itself for 'NAME' and 'DOB' and gets the output.</p>

<p>You can do this:</p>

<pre><code>temp = "DateOFBirth"
text = [(0, 1, 'Blank', ''), (0, 3, 'NAME', 'Vance Trimble'), (5, 7, 'LOC', 'Harrison'), (7, 9, 'LOC', 'Arkansas'), (9, 14, 'DOB', 'July 6 , 1913')]
rel = []
for i in text:
    if 'NAME' in i:
        rel.append(i[i.index('NAME')+1])    
        rel.append(temp)    
    elif 'DOB' in i:
        rel.append(i[i.index('DOB')+1])         
print rel
# result:
# ['Vance Trimble', 'DateOFBirth', 'July 6 , 1913']
</code></pre>

<p>In this way the results are independent of the location of the items 'NAME' and 'DOB' in the tuples, but only if e.g. the actual name always follows the 'tag' <code>'NAME'</code>, as it is here: <code>(0, 3, 'NAME', 'Vance Trimble')</code>, where the actual name follows the <code>NAME</code>.</p>

python list python-3.x