for(each) element aliasing

Reinhold Birkenfeld reinhold-birkenfeld-nospam at wolke7.net
Mon Nov 22 19:34:01 CET 2004


Matija Papec wrote:
> X-Ftn-To: Jp Calderone 
> 
> Jp Calderone <exarkun at divmod.com> wrote:
>>> -------
>>> 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
> 
> I guess this works like "map" but "for" is more idimatic.

Yes. List comprehensions are elegant (and fast) expressions you would
otherwise construct with map() and filter().

Reinhold

-- 
[Windows ist wie] die Bahn: Man muss sich um nichts kuemmern, zahlt fuer
jede Kleinigkeit einen Aufpreis, der Service ist mies, Fremde koennen
jederzeit einsteigen, es ist unflexibel und zu allen anderen Verkehrs-
mitteln inkompatibel.               -- Florian Diesch in dcoulm



More information about the Python-list mailing list