how to create a big list of list

Ben Finney ben+python at
Fri Jun 5 04:34:26 EDT 2009

command.bbs at (§ä´M¦Û¤vªº¤@¤ù¤Ñ) writes:

> if i want to create a list of list which size is 2**25
> how should i do it?
> i have try [ [] for x in xrange(2**25) ]
> but it take too long to initial the list
> is there any suggestion?

What is it you want to do with the result?

If you want to lazy-evaluate the expression, what you're looking for is
a generator. You can get one easily by writing a generator expression:

    >>> foo = ([] for x in xrange(2**25))
    >>> foo
    <generator object at 0x103dc788>
    >>> for item in foo:
    ...     do_interesting_stuff_with(item)

If what you want is to have a huge multi-dimensional array for numerical
analysis, lists may not be the best option. Instead, install the
third-party NumPy library and use its types. You don't show what kind of
data you want in your array, but assuming you want integers initialised
to zero:

    >>> import numpy
    >>> foo = numpy.zeros((2**25, 0), int)
    >>> foo
    array([], shape=(33554432, 0), dtype=int32)

Other quick ways of constructing NumPy arrays exist, see

 \           “Prediction is very difficult, especially of the future.” |
  `\                                                       —Niels Bohr |
_o__)                                                                  |
Ben Finney

More information about the Python-list mailing list