possibly trivial newbie list/array question

John Beppu beppu at binq.org
Thu Aug 23 10:17:09 EDT 2001


[  date  ] 2001/08/23 | Thursday | 04:39 AM
[ author ] Paul Rubin <phr-n2001 at nightsong.com> 

> Can you tell me the actual purpose of list comprehension?  
> 
>         [f(x) for x in a]
> 
> just seems like confusing syntactic hair that's otherwise equivalent
> to, though as we've seen sometimes slower than,
> 
>         map(lambda x: f(x), a).
> 
> Am I missing something?  Unless there's more to it than I see (which
> is quite possible), I don't understand why this feature made it into
> the language.

<disclaimer> I'm a perl programmer, <disclaimer> but there are
similar constructs in perl, and (in perl) the difference is this.

    * map creates a new list while iterating over a list
    * for (or foreach) just iterates over a list

I don't know if this is the case in python, but in perl, people
are strongly discouraged from using map when the list it generates
is not being used.  When you just want to iterate over a list,
use for or foreach, instead of map.

Again, this is for perl, and I don't know if it applies to python,
but I figured it might be informative.



-- 
package wuv'apqvjgt;($_=join('',(*PgtnHcemgt))) # print map "beppu\@$_\n", qw(
=~ s/([HaP])(?!e)/ \U>$1/g;s/^.|:| (?=A)|>//g;y # cpan.org  lbox.org  binq.org
/c-z/a-u/;print"J$_\n";#$^%$^X at .^ <!-- japh --> # oss.lineo.com codepoet.org);




More information about the Python-list mailing list