for(each) element aliasing

Jp Calderone exarkun at divmod.com
Mon Nov 22 11:39:18 EST 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";
> 
> and get the result
> -------
> 2 4 6
> 

  No.  Integers in Python are immutable.  For lists of other things, mutation in-place (similar to the above example) is possible, but for integers and other immutable types, it is not.  You may be interested in list comprehensions, though:

    arr = range(1, 4)
    arr[:] = [a * 2 for a in arr]
    print arr

  Jp



More information about the Python-list mailing list