[Tutor] Omitting lines matching a list of strings from a file

Alan Gauld alan.gauld at btinternet.com
Thu Feb 25 23:21:30 CET 2010

"Christian Witts" <cwitts at compuscan.co.za> wrote

> lines = [line for line in infile if line[146:148] not in omit_states]
> print ''.join(lines)
> Just remember that doing a list comprehension like that on a large file 
> will drastically reduce the speed of your application as well as 
> introduce memory bloat.

Given he was originally doing an explicit for loop over the file I doubt if
the comprehension will be any slower. But it may well use  more memory.

Alan G. 

More information about the Tutor mailing list