News for hackers. Stuff that matters.
Re:Code Like a Pythonista: Idiomatic Python by Goladus on Saturday October 22, 2011 @08:26PM (#3144711)

    x = [0] * 100
Re:Code Like a Pythonista: Idiomatic Python by daeken on Saturday October 22, 2011 @09:36PM (#3144859)
Just make sure that when you do this, you're not initializing it with mutable objects, e.g. lists.

    x = [[0]] * 2
    print x[0], x[1] # prints [0] [0]
    x[0].append(1)
    print x[0], x[1] # prints [0, 1] [0, 1]
That's because the objects inside the repeated list are simply references to the same object internally.
Re:Code Like a Pythonista: Idiomatic Python by zmmmmm on Saturday October 22, 2011 @08:38PM (#3144736)
Thanks! At first I wondered about efficiency but it seems like I can allocate lists with tens of millions of elements this way without any spike in CPU so it is obviously implemented efficiently. I knew there would be a better way :-)
Re:Code Like a Pythonista: Idiomatic Python by Ixiaus on Saturday October 22, 2011 @08:46PM (#3144761)
This is much better than my own comment - I never even knew about that syntax!

Is it efficient though?

Re:Code Like a Pythonista: Idiomatic Python by Goladus on Saturday October 22, 2011 @08:55PM (#3144778)
My guess is that it's optimized, but I don't know. Semantics-wise, it's consistent with list addition.

    >>> [0] + [0] + [0]
    [0, 0, 0]
    >>> [0] * 3
    [0, 0, 0]
Re:Code Like a Pythonista: Idiomatic Python by Ixiaus on Saturday October 22, 2011 @09:05PM (#3144801)
I just read, that syntax is sugar for the list.extend() method so it's still making copies of the list - I wouldn't use it for a list containing complex objects; but that is very idiomatic for simple use cases (like this one?).
Re:Code Like a Pythonista: Idiomatic Python by calpaterson on Sunday October 23, 2011 @05:08AM (#3145667)
It isn't syntax - help(list.__mul__)