the most efficient method of adding elements to the list

bruno at modulix onurb at xiludom.gro
Tue Jun 6 04:55:37 EDT 2006


alf wrote:
> Hi,
> 
> Would it be .append()? Does it reallocate te list with each apend?
> 
> l=[]
> for i in xrange(n):
>       l.append(i)
> 

<dumb>
FWIW, you'd have the same result with:
l = range(n)
</dumb>


More seriously (and in addition to other anwsers): you can also
construct a list in one path:

  l = [i for i in xrange(n)]

or if you want operations and conditionals :

  l = [trasform(i) for i in xrange(n) if match_some_cond(i)]



HTH
-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list