I have a very large CSV file with the following structure:
category,value
A,1
A,4
B,2
A,1
B,3
...
What I need are two lists. The first list contains all values from category A
, the seconds list contains all values from category B
.
A working solution:
import csv
list_a = []
list_b = []
with open('my_file.csv', mode='r') as f:
reader = csv.DictReader(f)
for line in reader:
if line['category'] == 'A':
list_a.append(line['value'])
if line['category'] == 'B':
list_b.append(line['value'])
Since the CSV file is so large, I would like to avoid the expensive append
calls. Is there a more efficient way?