Solution to your problem could be simple dictionary comprehension:
>>> Entry = namedtuple('Entry', 'Name, Date, Color')
>>> [l for l in open('t.tsv', 'r')]
<<<
['Name Date Color\n',
'Ray May Gray\n',
'Alex Apr Green\n',
'Ann Jun Blue\n',
'Kev Mar Gold\n',
'Rob May Black\n']
>>> [l.split() for l in open('t.tsv', 'r')]
<<<
[['Name', 'Date', 'Color'],
['Ray', 'May', 'Gray'],
['Alex', 'Apr', 'Green'],
['Ann', 'Jun', 'Blue'],
['Kev', 'Mar', 'Gold'],
['Rob', 'May', 'Black']]
>>> [Entry(*l.split()) for l in open('t.tsv', 'r')]
<<<
[Entry(Name='Name', Date='Date', Color='Color'),
Entry(Name='Ray', Date='May', Color='Gray'),
Entry(Name='Alex', Date='Apr', Color='Green'),
Entry(Name='Ann', Date='Jun', Color='Blue'),
Entry(Name='Kev', Date='Mar', Color='Gold'),
Entry(Name='Rob', Date='May', Color='Black')] >>> {'fooo':e for e in Entry(*l.split()) for l in open('t.tsv', 'r')}
>>> {e.Name:e for e in list(Entry(*l.split()) for l in open('t.tsv', 'r'))}
<<<
{'Alex': Entry(Name='Alex', Date='Apr', Color='Green'),
'Ann': Entry(Name='Ann', Date='Jun', Color='Blue'),
'Kev': Entry(Name='Kev', Date='Mar', Color='Gold'),
'Name': Entry(Name='Name', Date='Date', Color='Color'),
'Ray': Entry(Name='Ray', Date='May', Color='Gray'),
'Rob': Entry(Name='Rob', Date='May', Color='Black')}
I think you are thinking on reading the first row as header names. Python has DictReader - https://docs.python.org/2/library/csv.html#csv.DictReader
>>> import csv
>>> for line in csv.DictReader(open('t.tsv')): print line # don't forget to make your file coma-separated.
{'Date': 'May', 'Color': 'Gray', 'Name': 'Ray'}
{'Date': 'Apr', 'Color': 'Green', 'Name': 'Alex'}
{'Date': 'Jun', 'Color': 'Blue', 'Name': 'Ann'}
{'Date': 'Mar', 'Color': 'Gold', 'Name': 'Kev'}
{'Date': 'May', 'Color': 'Black', 'Name': 'Rob'}
or with dictionary comprehension:
>>> { line['Name']: line for line in csv.DictReader(open('t.tsv')) }
<<<
{'Alex': {'Color': 'Green', 'Date': 'Apr', 'Name': 'Alex'},
'Ann': {'Color': 'Blue', 'Date': 'Jun', 'Name': 'Ann'},
'Kev': {'Color': 'Gold', 'Date': 'Mar', 'Name': 'Kev'},
'Ray': {'Color': 'Gray', 'Date': 'May', 'Name': 'Ray'},
'Rob': {'Color': 'Black', 'Date': 'May', 'Name': 'Rob'}}
>>> rows_by_name = { line['Name']: line for line in csv.DictReader(open('t.tsv')) }
>>> rows_by_name['Ann']
<<< {'Color': 'Blue', 'Date': 'Jun', 'Name': 'Ann'}
If you want random samples - i suggest first reading a rows into list and then make selection through randbom module. Or... let's do it with Entry:
>>> rows = list(Entry(*l.split()) for l in open('t.tsv', 'r'))
>>> import random
>>> random.sample(rows, 1)
<<< [Entry(Name='Ray', Date='May', Color='Gray')]
>>> random.sample(rows, 1)
<<< [Entry(Name='Alex', Date='Apr', Color='Green')]
>>> random.sample(rows, 1)
<<< [Entry(Name='Name', Date='Date', Color='Color')]
>>> random.sample(rows, 1)
<<< [Entry(Name='Alex', Date='Apr', Color='Green')]
>>> random.sample(rows, 1)
<<< [Entry(Name='Alex', Date='Apr', Color='Green')]
>>> random.sample(rows, 1)
<<< [Entry(Name='Alex', Date='Apr', Color='Green')]
>>> random.sample(rows, 3)
<<<
[Entry(Name='Ray', Date='May', Color='Gray'),
Entry(Name='Kev', Date='Mar', Color='Gold'),
Entry(Name='Ann', Date='Jun', Color='Blue')]
>>> random.sample(rows, 3)
<<<
[Entry(Name='Ann', Date='Jun', Color='Blue'),
Entry(Name='Rob', Date='May', Color='Black'),
Entry(Name='Name', Date='Date', Color='Color')]
>>> random.sample(rows, 3)
<<<
[Entry(Name='Rob', Date='May', Color='Black'),
Entry(Name='Ann', Date='Jun', Color='Blue'),
Entry(Name='Kev', Date='Mar', Color='Gold')]
but beware, that you can load up your memory too much.