Re: "For" loop and list comprehension similarity
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Re: "For" loop and list comprehension similarity

From: Ben Finney <bignose+hates-spam@benfinney.id.au>
Date: Mon Mar 27 2006 - 00:40:56 CEST

s.lipnevich@gmail.com writes:

> On more than one occasion, I found myself wanting to use a "conditional
> loop" like this (with "Invalid syntax" error, of course):
>
> for i in c if <test>:
> print i*2
>
> ...because it's similar to the list comprehension construct:
>
> [i*2 for i in c if <test>]

Why not combine the two:

    for i in [j for j in c if <test>]:
        print i*2

-- 
 \       "I got food poisoning today. I don't know when I'll use it."  |
  `\                                                  -- Steven Wright |
_o__)                                                                  |
Ben Finney
Received on Sun Apr 30 21:15:34 2006