List replication operator

bartc bc at freeuk.com
Fri May 25 08:36:58 EDT 2018


On 24/05/2018 19:17, Steven D'Aprano wrote:
> But what do people think about proposing a new list replication with copy
> operator?
>> [[]]**5
>> would return a new list consisting of five shallow copies of the inner
> list.
>> Thoughts?

Choice of ** doesn't seem right for a start, as it suggests it should 
mean []*[]*[]*[]*[], which it doesn't. (Apparently []*2 /is/ the same as 
[]+[].)
How about just:
 x = dupllist([[]], 5)
 x[0].append(777)
 print (x)
which gives:
 [[777], [], [], [], []]
Of course you have to implement dupllist(), but you'd have to implement 
** too, and that is harder. For this specific example, it can just be:
def dupllist(x,n):
 return [x[0].copy() for _ in range(n)]
-- 
bartc


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /