Creating a list with holes

Chris Angelico rosuav at gmail.com
Fri Jan 3 10:57:56 EST 2014


On Sat, Jan 4, 2014 at 2:51 AM, Roy Smith <roy at panix.com> wrote:
> In article <mailman.4853.1388763434.18130.python-list at python.org>,
>  Chris Angelico <rosuav at gmail.com> wrote:
>
>> Alternatively, if you expect to fill in most of the elements, it's
>> possible you'd be happier working with a subclass of list that
>> auto-expands by filling in the spare space with a singleton meaning
>> "no element here".
>
> And, if you know ahead of time the maximum number of elements you will
> ever have:
>
> x = [None] * max_count
>
> will preallocate them all with a minimum of fuss.

Yes, as I use in the trivial example in the subsequent post. But as a
general solution this is usually insufficient. (Whether or not None is
valid as a sentinel is, of course, quite orthogonal to the discussion.
I avoided assuming that it was, the OP's now shown that it does seem
to be.)

ChrisA



More information about the Python-list mailing list