pepoluan
pepoluan

Reputation: 6780

Efficiently extract lines containing a string in Python

Sometimes I need to get only lines containing a certain string from a text file (e.g., while parsing a logfile). I usually do it this way:

with open(TEXTFILENAME,'r') as f:
  contents = f.readlines()

targets = [s for s in contents if FINDSTRING in s]

However, I saw there's a possible two-liner:

with open(TEXTFILENAME,'r') as f:
  targets = [s for s in f.readlines() if FINDSTRING in s]

I wonder if the second method is more efficient, whether the readlines() function in this case act as an iterator of sorts.

Upvotes: 0

Views: 10107

Answers (1)

inspectorG4dget
inspectorG4dget

Reputation: 113945

Avoid the call to readlines, which generates a list of all the lines. This should therefore be faster

with open(TEXTFILENAME,'r') as f:
    targets = [line for line in f if FINDSTRING in line]

Upvotes: 2

Related Questions