Initialize a list of objects in Python

Viewed 64726

I'm a looking to initialize an array/list of objects that are not empty -- the class constructor generates data. In C++ and Java I would do something like this:

Object lst = new Object[100];

I've dug around, but is there a Pythonic way to get this done?

This doesn't work like I thought it would (I get 100 references to the same object):

lst = [Object()]*100

But this seems to work in the way I want:

lst = [Object() for i in range(100)]

List comprehension seems (intellectually) like "a lot" of work for something that's so simple in Java.

4 Answers
Related