Read a file, skip unwanted lines & add into a List

I have the following code where I scan every line and put in a list. If the line matches a string "New changes", I don't want to put in the list. Any suggestion on how to achieve this?

with open('file.txt', 'rb') as f:
    mainlist = [line.strip() for line in f]

Answers


List comprehensions can also do filtering:

mainlist = [line.strip() for line in f if "New changes" not in line]

You can filter within the list comprehension :

mainlist = [line.strip() for line in f if line.strip() !=  "New changes"]

Need Your Help

can we customize facebook sdk framework?

ios facebook facebook-graph-api facebook-login facebook-ios-sdk

can we customize the login view in facebook sdk framework .Can we put our own button in the place of login button.I got this code from developer.apple.con ->samples->scrumptions.

c# create or modify file/directory permission: add group read permission using asp.net with mono and apache2

c# linux mono apache2 file-permissions

How can I change/set permissions of a file or directory using asp.net under mono and apache2?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.