Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'diff': 0.07; 'none,': 0.07; "'a'": 0.09; '-larry': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'try:': 0.09; 'used.': 0.09; 'def': 0.12; 'wrote': 0.14; '[none,': 0.16; 'defaultdict': 0.16; 'different,': 0.16; 'indexerror:': 0.16; 'interest,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'skip:n 70': 0.16; 'language': 0.16; '>>>': 0.22; 'creating': 0.23; 'question': 0.24; 'header:X-Complaints-To:1': 0.27; 'subject:list': 0.30; "i'm": 0.30; 'asked': 0.31; 'pos': 0.31; 'allows': 0.31; 'class': 0.32; 'lists': 0.32; 'thanks!': 0.32; 'skip:_ 10': 0.34; 'received:co.za': 0.34; 'received:za': 0.34; "i'd": 0.34; 'subject:with': 0.35; 'except': 0.35; 'something': 0.35; 'no,': 0.35; 'but': 0.35; 'there': 0.35; 'list': 0.37; 'e.g.': 0.38; 'to:addr:python-list': 0.38; 'previous': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'ago,': 0.61; 'answer.': 0.68; 'frank': 0.68; 'received:41': 0.70; 'holes': 0.84 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: "Frank Millman" Subject: Re: Creating a list with holes Date: Sat, 4 Jan 2014 09:00:22 +0200 References: X-Gmane-NNTP-Posting-Host: 41-133-115-190.dsl.mweb.co.za X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.3790.4657 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.4913 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 47 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1388818829 news.xs4all.nl 2930 [2001:888:2000:d::a6]:56896 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:63114 "Larry Martell" wrote in message news:CACwCsY5P47-dB1NLQTUTQ=0aF6B+-M3y4hCxcUGmcVmHM8=-xQ@mail.gmail.com... >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 ... ) > > Thanks! > -larry Just out of interest, I asked the same question on this list many years ago, and someone actually gave me an answer. It was something like the following - >>> class MyList(list): ... def __getitem__(self, pos): ... try: ... return list.__getitem__(self, pos) ... except IndexError: ... return None ... def __setitem__(self, pos, value): ... try: ... list.__setitem__(self, pos, value) ... except IndexError: ... diff = pos - list.__len__(self) ... self.extend([None] * diff) ... self.append(value) ... >>> ml = MyList() >>> ml[3] >>> ml[3] = 'a' >>> ml [None, None, None, 'a'] >>> I wanted it because I was familiar with it from a previous language I had used. As is turns out, I never actually used it, but I was impressed! Frank Millman