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


Groups > comp.lang.python > #63062

Re: Creating a list with holes

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.012
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'assign': 0.07; 'element': 0.07; 'list?': 0.07; 'iterate': 0.09; 'cc:addr:python-list': 0.11; 'jan': 0.12; "wouldn't": 0.14; 'defaultdict': 0.16; 'different,': 0.16; 'elements,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'singleton': 0.16; 'subclass': 0.16; 'write.': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'not,': 0.20; 'cc:addr:python.org': 0.22; 'creating': 0.23; 'exists': 0.24; 'cc:2**0': 0.24; 'options': 0.25; 'order.': 0.26; 'header:In- Reply-To:1': 0.27; 'function': 0.29; 'am,': 0.29; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'larry': 0.31; 'allows': 0.31; 'lists': 0.32; 'probably': 0.32; 'run': 0.32; 'maybe': 0.34; "i'd": 0.34; 'subject:with': 0.35; 'something': 0.35; 'no,': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'possible': 0.36; 'list': 0.37; 'e.g.': 0.38; 'list,': 0.38; 'expect': 0.39; 'space': 0.40; 'most': 0.60; 'worth': 0.66; 'filling': 0.78; 'case?': 0.84; 'dict.': 0.84; 'different.': 0.84; 'holes': 0.84; "it'd": 0.84; 'yours': 0.88; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=6DqBJikBfmc9HBQ1r4zNkEckeY30ZjksjgLSxi2bpek=; b=Qr0lUy8qIcqjqEVVBgI0HBv16yt5xkAQmFpHi57uy+T3s+mHw8RTak9ZShTDtYp5tr nZE3cX/5y2QoWoO8qaUwnr+xeM76tjj37UN7stLYRDBsfM8as5DpBAe5lr8oR2VM4qkg qdAWDZCto24N8aWIbUjF7ZRxr+si9CwD+UtP1T2sO3ZGVWcqSFWFSRk/pjIG95qnQvTh WIPngZHeI0rZDB24DzkCPMg0p+n66J3dduqIfHoccGjKTGMD71DQe1ttuLu6RbpHHtqn QrpIYqU8SdJh34FQceVQafFxMa6bRJv6wXxxmOfc9XVKwZP+4k7hdJe1sDlLjgnfpuFF 2YLQ==
MIME-Version 1.0
X-Received by 10.68.196.193 with SMTP id io1mr96111869pbc.46.1388763425312; Fri, 03 Jan 2014 07:37:05 -0800 (PST)
In-Reply-To <CACwCsY5P47-dB1NLQTUTQ=0aF6B+-M3y4hCxcUGmcVmHM8=-xQ@mail.gmail.com>
References <CACwCsY5P47-dB1NLQTUTQ=0aF6B+-M3y4hCxcUGmcVmHM8=-xQ@mail.gmail.com>
Date Sat, 4 Jan 2014 02:37:05 +1100
Subject Re: Creating a list with holes
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4853.1388763434.18130.python-list@python.org> (permalink)
Lines 26
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1388763434 news.xs4all.nl 2839 [2001:888:2000:d::a6]:53565
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:63062

Show key headers only | 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