Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #63062

Re: Creating a list with holes

References <CACwCsY5P47-dB1NLQTUTQ=0aF6B+-M3y4hCxcUGmcVmHM8=-xQ@mail.gmail.com>
Date 2014-01-04 02:37 +1100
Subject Re: Creating a list with holes
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.4853.1388763434.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Sat, Jan 4, 2014 at 2:19 AM, Larry Martell <larry.martell@gmail.com> wrote:
> I think I know the answer is no, but is there any package that allows
> creating a list with holes in it? E.g. I'd want to do something like:
>
> x[10] = 12
> x[20] = 30
>
> I'm thinking of something like defaultdict but for lists (I know
> that's very different, but ... )

Depending on what exactly you need, it's probably worth just using a
dict. In what ways do you need it to function as a list? You can
always iterate over sorted(some_dict.keys()) if you need to run
through them in order.

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". The code probably exists somewhere, but if not, it
wouldn't be hard to write. Then it'd be a list, but you can start with
it empty and assign as you describe above.

What's the use case? I expect that one or the other of those options
would cover most cases, but maybe yours is different.

ChrisA

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

Re: Creating a list with holes Chris Angelico <rosuav@gmail.com> - 2014-01-04 02:37 +1100
  Re: Creating a list with holes Roy Smith <roy@panix.com> - 2014-01-03 10:51 -0500
    Re: Creating a list with holes Chris Angelico <rosuav@gmail.com> - 2014-01-04 02:57 +1100

csiph-web