Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > 'while' in list comprehension?

Reply
Thread Tools

'while' in list comprehension?

 
 
jsaul
Guest
Posts: n/a
 
      10-22-2003
Hi there,

wouldn't it be useful to have a 'while' conditional in addition to
'if' in list comprehensions?

foo = []
for i in bar:
if len(i) == 0:
break
foo.append(i)

would then turn into

foo = [ i for i in bar while len(i)>0 ]

Is there any reason for not having this kind of thing? I actually
miss it pretty often.

Cheers, jsaul
 
Reply With Quote
 
 
 
 
Emile van Sebille
Guest
Posts: n/a
 
      10-22-2003
jsaul asks...
> would then turn into
>
> foo = [ i for i in bar while len(i)>0 ]
>
> Is there any reason for not having this kind of thing? I actually
> miss it pretty often.
>



How is this different from:

foo = [ i for i in bar if len(i) ]

Emile van Sebille
http://www.velocityreviews.com/forums/(E-Mail Removed)


 
Reply With Quote
 
 
 
 
Skip Montanaro
Guest
Posts: n/a
 
      10-22-2003

>> foo = [ i for i in bar while len(i)>0 ]


Emile> How is this different from:

Emile> foo = [ i for i in bar if len(i) ]

The first is like:

_ = []
for i in bar:
if not (len(i) > 0):
break
_.append(i)
return _

The second is like:

_ = []
for i in bar:
if len(i) > 0:
_.append(i)
return _

Skip

 
Reply With Quote
 
Bob Gailer
Guest
Posts: n/a
 
      10-22-2003
At 01:05 PM 10/22/2003, Emile van Sebille wrote:

>jsaul asks...
> > would then turn into
> >
> > foo = [ i for i in bar while len(i)>0 ]
> >
> > Is there any reason for not having this kind of thing? I actually
> > miss it pretty often.
> >

>
>
>How is this different from:
>
> foo = [ i for i in bar if len(i) ]


My reading is that the comprehension would stop at the first i whose len
were 0. e.g.
foo = []
for i in bar:
if len(i) == 0:break
foo.append(i)

Bob Gailer
(E-Mail Removed)
303 442 2625


---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.525 / Virus Database: 322 - Release Date: 10/9/2003

 
Reply With Quote
 
Jeff Epler
Guest
Posts: n/a
 
      10-22-2003
Use 2.3's itertools:
foo = [i for i in itertools.takewhile(lambda i: len(i) > 0, bar)]
or maybe
foo = list(itertools.takewhile(len, bar)]
or even
foo = itertools.takewhile(len, bar) # an iterable, not a list

Jeff

 
Reply With Quote
 
Francis Avila
Guest
Posts: n/a
 
      10-22-2003
"Emile van Sebille" <(E-Mail Removed)> wrote in message
news:bn6k9i$tftt3$(E-Mail Removed)-berlin.de...
> jsaul asks...
> > would then turn into
> >
> > foo = [ i for i in bar while len(i)>0 ]
> >
> > Is there any reason for not having this kind of thing? I actually
> > miss it pretty often.
> >

>
>
> How is this different from:
>
> foo = [ i for i in bar if len(i) ]
>
> Emile van Sebille
> (E-Mail Removed)
>


The idea is that 'while' stops iterating through the list when the condition
isn't met. 'if' just doesn't output anything. e.g.:

>>> bar = ['this', 'is', 'a', 'list', '', 'see?']
>>> [i for i in bar if len(i)]

['this', 'is', 'a', 'list', 'see?']
>>> [i for i in bar while len(i)] # Pretend

['this', 'is', 'a', 'list']

What's your typical use for this?

I guess I see nothing really wrong with it, although I thought list
comprehensions were supposed to make the iteration transparent--'while'
kinda destroys the illusion. But these are the only two looping constructs
that make sense in a list comprehension, so why not support both? OTOH,
'while' makes no sense in a dictionary comprehension (and presumably we'll
have those one day.)

 
Reply With Quote
 
Hung Jung Lu
Guest
Posts: n/a
 
      10-23-2003
jsaul <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
>
> foo = [ i for i in bar while len(i)>0 ]
>
> Is there any reason for not having this kind of thing? I actually
> miss it pretty often.


Could you provide an example where you need it?

From your message, it seems "bar" might be a list of strings. If so, you could use:

foo = bar[:bar.index('')]

regards,

Hung Jung
 
Reply With Quote
 
Terry Reedy
Guest
Posts: n/a
 
      10-23-2003

"jsaul" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi there,
>
> wouldn't it be useful to have a 'while' conditional in addition to
> 'if' in list comprehensions?
>
> foo = []
> for i in bar:
> if len(i) == 0:
> break
> foo.append(i)
>
> would then turn into
>
> foo = [ i for i in bar while len(i)>0 ]


while is simply not same as if: break!
if executes once for each value of i, while indefinitely.
If you translate back by current rule, which will not change, you get:

foo = []
for i in bar:
while len(i) >0:
foo.append(i)

Terry J. Reedy


 
Reply With Quote
 
Stephen Horne
Guest
Posts: n/a
 
      10-23-2003
On Wed, 22 Oct 2003 22:57:42 -0400, "Terry Reedy" <(E-Mail Removed)>
wrote:

>while is simply not same as if: break!
>if executes once for each value of i, while indefinitely.
>If you translate back by current rule, which will not change, you get:
>


'while' is an English word which has meaning independant of the
existing Python 'while' loop. It is not necessarily wrong to apply a
different aspect of that meaning in a list comprehension.

Besides, I read the syntax as equating to...

foo = []
for i in bar while len(i) >0:
foo.append(i)

Yes, I know that isn't a legal Python loop. My point is that it didn't
look like a nested loop to me, but rather like an additional qualifier
on the existing loop.

That said, you do have a point - multiple 'for' parts in a list
comprehension act as nested loops, so maybe a while part should too.

The trouble is that a standalone while loop probably makes little
sense in a list comprehension - sure you have a place to put the loop
condition, but what about the initialisation and body?

If there were a real while-part in a list comprehension, it would
probably need those things to become explicit (becoming a lot like the
C for loop) - something like...

[k while k=1; k<1024; k*=2]

Hmmmm...

while i=0; i<10; i++ :
print i

Hmmmm....

Nah - damn silly idea.


--
Steve Horne

steve at ninereeds dot fsnet dot co dot uk
 
Reply With Quote
 
jsaul
Guest
Posts: n/a
 
      10-24-2003
* Terry Reedy [2003-10-23 04:57]:
> "jsaul" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > wouldn't it be useful to have a 'while' conditional in addition to
> > 'if' in list comprehensions?
> >
> > foo = []
> > for i in bar:
> > if len(i) == 0:
> > break
> > foo.append(i)
> >
> > would then turn into
> >
> > foo = [ i for i in bar while len(i)>0 ]

>
> while is simply not same as if: break!
> if executes once for each value of i, while indefinitely.
> If you translate back by current rule, which will not change, you get:
>
> foo = []
> for i in bar:
> while len(i) >0:
> foo.append(i)


I agree that 'while' cannot not just be considered a replacement
for 'if'. However, adding a 'while' conditional to list
comprehensions would very unlikely be misunderstood as another
(infinite) loop. Instead, I find the above 'while' example about
as intuitive as is the case with 'if'. It simply means that under
a certain condition the loop will be ended, which is just what
most people would probably expect.

Anyway, thanks a lot to all who replied! What is your opinion
after this discussion, write a PEP or just forget about it?

Cheers, jsaul
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Collections List : Converting from List '<Column <String1,String2>>' to 'List <String1>' asil klin Java 28 03-05-2011 01:59 AM
Memory issues when storing as List of Strings vs List of List OW Ghim Siong Python 2 11-30-2010 12:22 PM
Appending a list's elements to another list using a list comprehension Debajit Adhikary Python 17 10-18-2007 06:45 PM
Why does list.__getitem__ return a list instance for subclasses ofthe list type? dackz Python 0 02-06-2007 04:44 PM
Difference Between List x; and List x(); , if 'List' is a Class? roopa C++ 6 08-27-2004 06:18 PM



Advertisments