How do I initialize a list please?

Christian Tismer tismer at tismer.com
Tue Dec 31 17:17:47 EST 2002


Christopher wrote:
> Gerhard Häring <gerhard.haering at gmx.de> wrote in message news:<mailman.1041347835.14601.python-list at python.org>...
> 
>>* OUTLand <cmfinlay at SPAMmagnet.com.au> [2003-01-01 02:07 +1100]:
>>
>>>How do I initialize a list please?
...

>>dim = [1.0] * 10
>>
>>But don't do this for two-dimensional arrays:
...

>>>>dim = []
>>>>for x in range(10):
>>>
> ... 	dim.append([1.0] * 10)
> ... 	
> 
> Or is there an easier, more efficient way?

Semantically equivalent, but probably the standard
way for Python with list comprehensions, you would use

 >>> dim = [ [1.0] * 10 for i in xrange(10) ]

Happy new year -- chris

-- 
Christian Tismer             :^)   <mailto:tismer at tismer.com>
Mission Impossible 5oftware  :     Have a break! Take a ride on Python's
Johannes-Niemeyer-Weg 9a     :    *Starship* http://starship.python.net/
14109 Berlin                 :     PGP key -> http://wwwkeys.pgp.net/
work +49 30 89 09 53 34  home +49 30 802 86 56  pager +49 173 24 18 776
PGP 0x57F3BF04       9064 F4E1 D754 C2FF 1619  305B C09C 5A3B 57F3 BF04
      whom do you want to sponsor today?   http://www.stackless.com/






More information about the Python-list mailing list