Loop Backwards

Felipe Almeida Lessa felipe.lessa at gmail.com
Tue Feb 14 06:08:22 EST 2006


Em Ter, 2006-02-14 às 10:08 +0100, bruno at modulix escreveu:
> for item in reversed(alist):
>   do_something_with(item)
> 
> or (more perlish at first sight):
> 
> for item in alist[::-1]:
>   do_something_with(item)

No "or" here. The [::-1] version creates a whole new list in memory,
it's silly to believe both will behave equally (well, strictly speaking
they will, but one will use twice more memory than the other).

-- 
"Quem excele em empregar a força militar subjulga os exércitos dos
outros povos sem travar batalha, toma cidades fortificadas dos outros
povos sem as atacar e destrói os estados dos outros povos sem lutas
prolongadas. Deve lutar sob o Céu com o propósito primordial da
'preservação'. Desse modo suas armas não se embotarão, e os ganhos
poderão ser preservados. Essa é a estratégia para planejar ofensivas."

  -- Sun Tzu, em "A arte da guerra"




More information about the Python-list mailing list