tags:

views:

199

answers:

3

In Python, if I multiply of list of objects by an integer, I get a list of references to that object, e.g.:

>>> a = [[]] * 3
>>> a
[[], [], []]
>>> a[0].append(1)
>>> a
[[1], [1], [1]]

If my desired behavior is to create a list of copies of the original object (e.g. copies created by the "copy.copy()" method or something sort of standard, is there an elegant way to do this with the same multiplication operator? Or should I just stick with a list comprehension or something? E.g.

[[] for x in range(0,3)]

Any version of Python is fine.

+4  A: 

This is a good usage of list comprehension - its also the most readable way to do it IMO.

Shane C. Mason
+1  A: 

The multiplication operator on a sequence means repetition of the item(s) -- NOT creation of copies (shallow or deep ones) of the items. Nothing stops you from going crazy, a la:

import copy

class Crazy(object):
  def __init__(self, body, weird=copy.copy):
    self.gomez = body
    self.cousinitt = weird
  def __mul__(self, n):
    return [self.cousinitt(x) for x in (self.gomez * n)]

a = Crazy([[]]) * 3

...except your sanity and common sense, if any. Checking on those, how DID you dream operator * could be made to mean something utterly different than it's intended to mean, except by defining another class overloading __mul__ in weird ways...?-)

Alex Martelli
While I understand your emphasis on this being weird, wouldn't it be easier (and more generally useful) to inherit from the `list` class so that we have all the grunt list-handling work done for us, and simply overload the `__mul__` operator to do copying?
Chris Lutz
A: 

The list comprehension is the best way to do this. If you define a new class and overload the * operator, it will seriously confuse the next person to read the code.

Nikwin