How do I initialize a list please?

Gerhard Häring gerhard.haering at gmx.de
Tue Dec 31 10:16:24 EST 2002


* OUTLand <cmfinlay at SPAMmagnet.com.au> [2003-01-01 02:07 +1100]:
> How do I initialize a list please?
> In Python
> I want to create a list with the same value each time.
> 
> my current way is
> 
> QUOTE
> dim = [1.0, 1.0, 1.0, 1.0, 1.0, \
>               1.0, 1.0, 1.0, 1.0, 1.0]
> UNQUOTE

dim = [1.0] * 10

But don't do this for two-dimensional arrays:

dim = [[1.0] * 10] * 10

as this will give you a list with ten references to the *same* inner
list.

Gerhard
-- 
Favourite database:             http://www.postgresql.org/
Favourite programming language: http://www.python.org/
Combine the two:                http://pypgsql.sf.net/
Embedded database for Python:   http://pysqlite.sf.net/




More information about the Python-list mailing list