Extended List Comprehension Syntax : List Comprehension « List « Python Tutorial






lines = [line.rstrip(  ) for line in open('script1.py') if line[0] == 'p']

res = []
for line in open('script1.py'):
    if line[0] == 'p':
        res.append(line.rstrip(  ))
print res








7.15.List Comprehension
7.15.1.List Comprehension
7.15.2.List Comprehensions in build Connection String, Step by Step
7.15.3.Filtering Lists: [mapping-expression for element in source-list if filter-expression]
7.15.4.List Type Operators and List Comprehensions
7.15.5.Extended List Comprehension Syntax
7.15.6.Using List Comprehensions on Files