0

I have a text file (tab delimeted) and I would like to pass field 1 in a list (list1) and field 3 in another list (list2) using list comprehension.

The code I wrote is this:

with open (path) as file1:
   list1 = [line.strip().split('\t')[0] for line in file1];
   list2 = [line.strip().split('\t')[3] for line in file1];

The problem is that list2 stays empty.

Any thoughts why this is happening?

Thanks!

  • @Bhargav Rao, the problem may be related but the answers in the dup but they are not really a good solution to the OP's problem – Padraic Cunningham Jul 07 '16 at 16:49
  • @PadraicCunningham, The issue *is* the same. It is certainly true that in this case we can find a way to avoid iterating twice. However, we can certainly add an answer to the target and hence make it a good canonical post to help others in future. (Aside, Congrats on 100k rep :).) – Bhargav Rao Jul 07 '16 at 16:55

1 Answers1

1

You need to reset the pointet back to the start of the file:

with open (path) as file1:
   list1 = [line.strip().split('\t')[0] for line in file1]
   file1.seek(0)
   list2 = [line.strip().split('\t')[3] for line in file1];

But you can do it in one pass, just append to each as you iterate so you only need to go over the file content once:

with open (path) as file1:
    list1, list2 = [], []
    for line in file1:
       spl =line.strip().split('\t')
       list1.append(spl[0])
       list2.append(spl[3])

You can also use the csv lib to parse the data for you:

import  csv

with open (path) as file1:
    list1, list2 = [], []
    for row in csv.reader(file1, delimiter="\t):
       list1.append(row[0])
       list2.append(row[3])
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
  • I upvoted your answer but it says it'll not be visible to other users because I'm a new user ! Anyway, It worked. Thanks for the quick response. – geoskouf Jul 07 '16 at 16:46
  • @geoskouf, no worries, the question was marked as a dupe but I don't there is no need to iterate over the file object twice or store all the lines with readlines – Padraic Cunningham Jul 07 '16 at 16:49
  • I thought that appending in an empty list is not a good idea! Isn't that right? They say it's better if you choose the more pythonic way (list comprehension in my situation)! – geoskouf Jul 07 '16 at 16:49
  • @geoskouf, if you had a huge file do you think iterating over it twice would be better than a single pass? – Padraic Cunningham Jul 07 '16 at 16:50
  • Of course not! So there is no way to create both lists in one pass using list comprehension right? I tried a couple of things but non of them worked. – geoskouf Jul 07 '16 at 16:55
  • @geoskouf, not without using a list comp for side effects. Sometimes a simple for loop is the best way to go. – Padraic Cunningham Jul 07 '16 at 17:01