[Tutor] question about strip() and list comprehension
Walter Prins
wprins at gmail.com
Wed Apr 9 00:11:10 CEST 2014
Hi,
On 8 April 2014 22:38, Jared Nielsen <nielsen.jared at gmail.com> wrote:
> Hello,
> Could someone explain why and how this list comprehension with strip()
> works?
>
> f = open('file.txt')
> t = [t for t in f.readlines() if t.strip()]
> f.close()
> print "".join(t)
>
> I had a very long file of strings filled with blank lines I wanted to
> remove. I did some Googling and found the above code snippet, but no clear
> explanation as to why it works. I'm particularly confused by how "if
> t.strip()" is removing the blank lines. I also don't fully understand the
> 'print "".join(t)'.
The list comprehension loops through each item in f.readlines(), and
outputs each item (adds it to the output list being constructed), if
and only if the "if" filter condition is true. Now, to understand "if
t.strip()" you need to understand that Python allows objects and items
which are not explicitly bool types in contexts where a boolean is
required, such as in if statements.
In the case of strings, a blank/empty string is considered False,
while a non-blank/empty string is considered True. As a consequence,
if t.strip() is equivalent to writing if t.strip() != '', and so its
presence effectively suppresses adding empty lines from the file into
the output list t. An empty string is said to be "falsy" while a
non-empty string is said to be "truthy". For more see Section 5.1
(Truth value testing), here:
https://docs.python.org/2/library/stdtypes.html
As for the question about the print statement, firstly read the
following documentation page that describes the string str.join()
method: https://docs.python.org/2/library/stdtypes.html#str.join
>From this, you should be able to infer that what "".join(t) does is to
effectively construct a new output string by concatenating all the
items in t, using a blank string as the delimiter, e.g. the result is
effectively to just concatenate all the strings directly with no
additional delimiter.
> The above didn't remove the leading white space on several lines, so I made
> the following addition:
>
> f = open('file.txt')
> t = [t for t in f.readlines() if t.strip()]
> f.close()
> s = [x.lstrip() for x in t]
> print "".join(s)
>
> List comprehensions are still magic to me. How would I go about
> incorporating lstrip() in the first list comprehension?
The output expression for each item output in the list comprehension,
that's the bit n front of the "for", is something you specify/control.
Now, in your original code, you just output the line that was read
from the file verbatim (e.g. "t"), but nothing forces this on you --
instead you can write any expression you like, including calling
"lstrip" on t, as in your question, e.g simply.
t = [t.lstrip() for t in f.readlines() if t.strip()]
HTH,
Walter
More information about the Tutor
mailing list