0

I have 2 CSV files of varying lengths. I'm looking for a pythonic way to loop over both of them, pulling data from predefined columns and append that data to my 2 list respectively.

So data from f1 goes to list_A and data from f2 goes to list_B

I realise using zip wont work as the number of rows in each file is different.

list_A = []
list_B = []


with open(file_1,'r') as f1, open(file_2,'r') as f2:
    csvFile_1 = csv.reader(f1, delimiter=',')
    csvFile_2 = csv.reader(f2, delimiter=',')

    for x,y in zip(csvFile_1, csvFile_2):
        list_A.append(x[1])
        list_B.append(y[0])

Any pointers appreciated..

1
  • Why iterate both as once ? any reason to not use pandas lib ?
    – azro
    Commented Nov 11, 2021 at 13:16

1 Answer 1

1

There is no reason to read them with zip, read them independently

with open(file_1, 'r') as f1, open(file_2, 'r') as f2:
    list_A = [x[1] for x in csv.reader(f1, delimiter=',')]
    list_B = [x[0] for x in csv.reader(f2, delimiter=',')]

Or use pandas library

import pandas as pd

list_A = pd.read_csv(file_1, sep=',').iloc[:, 1].to_list()
list_B = pd.read_csv(file_2, sep=',').iloc[:, 0].to_list()
3
  • Thank you kindly, that does the trick.. I didn't want to use pandas as it was overkill for what I needed but your first solution works great for me... Thanks again
    – blupacetek
    Commented Nov 11, 2021 at 14:27
  • @blupacetek pandas is not an overkill way, that is a performant lib and good even for small things
    – azro
    Commented Nov 11, 2021 at 14:28
  • Ok thanks ill give it a try :-)
    – blupacetek
    Commented Nov 11, 2021 at 15:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.