Python newbie

Peter Pearson ppearson at nowhere.invalid
Fri Sep 19 17:37:20 CEST 2008


On 19 Sep 2008 08:13:05 GMT, Steven D'Aprano  wrote:
> On Fri, 19 Sep 2008 09:13:48 +0200, Mladen Gogala wrote:
[snip]
>> perl -e '@a=(1,2,3); map { $_*=2 } @a; map { print "$_\n"; } @a;'
>> 
>> The equivalent in Python looks like this:
>
> Actually, no it doesn't. The equivalent looks more like this:
>
>
> $ python -c 'a=[1,2,3]; a=[2*x for x in a];
> for x in a:
>     print x'
> 2
> 4
> 6

Good job, SD'A.

A note for Mladen Gogala: the [...for...] construction in

  a=[2*x for x in a];

is called a list comprehension, and is wonderful for shortening
and clarifying code.  One of the pleasures of following this
group is watching the old hands answer noob questions with one
or two list comprehensions.

-- 
To email me, substitute nowhere->spamcop, invalid->net.



More information about the Python-list mailing list