for(each) element aliasing

F. Petitjean littlejohn.75 at noos.fr
Mon Nov 22 17:35:48 CET 2004


On Mon, 22 Nov 2004 17:21:26 +0100, Matija Papec <mpapec at yahoo.com> wrote:
> 
> I know how to alter the list via list indicies but I would like to know if
> there is posibility to write in Python something like:
> 
> my @arr = 1..3;
> for my $element (@arr) {
>   $element *= 2;
> }
> print "@arr\n";

For instance :
arr = 1, 2, 3  # range(1, 4)
arr = [ element*2 for element in arr ]
print arr   # [2, 4, 6]  it is a list
for element in arr:
    print element,  # note the comma
print  # useful if not in interactive mode only


Read the documentation about "list comprehension"
> 
> and get the result
> -------
> 2 4 6
> 
> 



More information about the Python-list mailing list