Sequence-spreading

Joshua Marshall jmarshal at mathworks.com
Tue Mar 20 14:33:27 EST 2001


gzeljko <gzeljko at sezampro.yu> wrote:

> Alex Martelli <aleaxit at yahoo.com> wrote in message
>> offsets = [ [] for i in range(pm) ]
>> for i in range(len(arg)):
>>     offsets[i%pm].append(i)
>>
>>
>> Alex
>>

> Old syntax yet works:

> offsets = [[]]*pm

Careful here:

 >>> x = [[]]*2
 >>> y = [[] for i in range(2)]
 >>> x
 [[], []]
 >>> y
 [[], []]
 >>> x[0] is x[1]
 1
 >>> y[0] is y[1]
 0

The first way gives you the same value at each element ("[]" is
evaluated once), while the second way gives you distinct values ("[]"
is evaluated for each element).



More information about the Python-list mailing list