[Tutor] a shorter way to write this
John Fouhy
jfouhy at paradise.net.nz
Sat Mar 26 22:09:36 CET 2005
Kent Johnson wrote:
> jrlen balane wrote:
>> basically, i'm going to create a list with 96 members but with only
>> one value:
>> is there a shorter way to write this one???
> [1] * 96
Just a note on this ---
This will work fine for immutable types (such as integers or strings).
But you can get into trouble if you do this with mutable types.
eg:
>>> l = [[]] * 10 # A list of ten empty lists
>>> l
[[], [], [], [], [], [], [], [], [], []]
>>> l[3].append(2)
>>> l
[[2], [2], [2], [2], [2], [2], [2], [2], [2], [2]]
--
John.
More information about the Tutor
mailing list