I've stumbled upon a problem I just can't seem to find an answer for. I am reading the contents of a csv file using csv.DictReader
and constructing a list of namedtuples from that information. However, upon running it, the end result is an empty list.
from collections import namedtuple
import csv
nt = namedtuple('suminfo', 'a, b, c')
checklist = ['check1', 'check2', 'check3']
with open('test.csv', 'r') as csv_file:
csv_rows = csv.DictReader(csv_file, delimiter='\t')
tups = [nt(row['first'], row['second'], row['third'])
for row in csv_rows if row['first'] in checklist]
I have also tried a typical normal loop of the rows followed by list appending of a namedtuple, and this seems to work just fine.
Why is the listcomprehension not behaving as expected? Thank you for your time.