Re: [Tutor] question about strip() and list comprehension

2014-04-09 Thread Peter Otten
Steven D'Aprano wrote: > On Tue, Apr 08, 2014 at 02:38:13PM -0600, Jared Nielsen 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

Re: [Tutor] question about strip() and list comprehension

2014-04-09 Thread Jared Nielsen
Thanks Danny! That was an awesome explanation. On Tue, Apr 8, 2014 at 7:05 PM, Danny Yoo wrote: > >> if line.strip() >> >> Is that stripping the line of white space at the same time that it is >> testing it? >> >> > > Two features about Python: > > 1. Strings are immutable, so the above is com

Re: [Tutor] question about strip() and list comprehension

2014-04-09 Thread Jared Nielsen
Thank Danny, That's much more clear. But I still don't understand what's happening with: if line.strip() Is that stripping the line of white space at the same time that it is testing it? On Tue, Apr 8, 2014 at 3:44 PM, Danny Yoo wrote: > > Could someone explain why and how this list comprehen

Re: [Tutor] question about strip() and list comprehension

2014-04-08 Thread Danny Yoo
> > > if line.strip() > > Is that stripping the line of white space at the same time that it is > testing it? > > Two features about Python: 1. Strings are immutable, so the above is computing what a whitespace-stripped line would look like. So that means that 'line.strip()' is doing just a com

Re: [Tutor] question about strip() and list comprehension

2014-04-08 Thread Steven D'Aprano
On Tue, Apr 08, 2014 at 02:38:13PM -0600, Jared Nielsen 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 fil

Re: [Tutor] question about strip() and list comprehension

2014-04-08 Thread Ben Finney
Jared Nielsen writes: > 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 The code you found is one of several syntactic shortcuts in Python, which allow creating a sequence directly from an expression in your cod

Re: [Tutor] question about strip() and list comprehension

2014-04-08 Thread Walter Prins
Hi, On 8 April 2014 22:38, Jared Nielsen 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

Re: [Tutor] question about strip() and list comprehension

2014-04-08 Thread Danny Yoo
> 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) Hi Jared, Let me rewrite this without the list comprehension, while preserving behavior. #

[Tutor] question about strip() and list comprehension

2014-04-08 Thread Jared Nielsen
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 co