Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: srinivas devaki Newsgroups: comp.lang.python Subject: Re: How To Create A Endles List Of Lists In Python...??? Date: Fri, 20 Nov 2015 23:58:20 +0530 Lines: 47 Message-ID: References: <564F1AC0.5040106@shopzeus.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de QAP/OnkThdUrdjUI0VGmYg8jhf9jNFeX3HCcAfvQsrVQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'main()': 0.07; 'cc:addr :python-list': 0.09; 'subject:How': 0.09; 'lambda:': 0.09; 'self.data': 0.09; 'url:github': 0.09; 'anyway': 0.11; 'def': 0.13; "'('": 0.16; '1)])': 0.16; 'main():': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'header:In-Reply- To:1': 0.24; 'chris': 0.26; 'fri,': 0.27; 'message- id:@mail.gmail.com': 0.27; "skip:' 10": 0.28; 'print': 0.30; 'skip:s 30': 0.31; 'skip:_ 10': 0.32; 'useful': 0.33; 'class': 0.33; 'problem': 0.33; 'subject:List': 0.33; 'similar': 0.33; 'skip:d 20': 0.34; 'received:google.com': 0.35; 'nov': 0.35; 'something': 0.35; 'received:209.85': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'data': 0.39; 'some': 0.40; 'here.': 0.62; '20,': 0.66; 'chrisa': 0.84; 'x):': 0.84; 'subject:Lists': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=dFUgtBtdO3uDBn/yQ/Fy6bP94OmJiwx2lPtpohGP824=; b=ZaB+ZSF5iG9tHt5w2AjLDJxzCmUZaiysbo6e2nI5Tu1a2h/QAtm6/5w4bZKk+Layfi ZpLd+uF3wzqcNdp+/+Ff8q0oPYhVozXOaBzLhf5ZPSyES/TvZYaNzhSfgTzhee3Ehze1 GrqjZd806drYtcF8hdtGnG7HPq3FuZiyvQgbXvLSc9R3Z1+vOle9U4HVvdNpnibFgV/u oR9QlQGrjMxWvlR5GlG6qsR1B0aJVDpo6oI15vpR34xibQIVOm/oB+f8Ys91r9OpDDRY ghdeDjBO6wBMl+x8ywBDIOWBgBnv4BX2kC6qn+rQCgjRr6U2YOkWkDpNVoPVPnI5ROCm liKg== X-Received: by 10.50.183.9 with SMTP id ei9mr1323506igc.81.1448044140217; Fri, 20 Nov 2015 10:29:00 -0800 (PST) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:99179 On Fri, Nov 20, 2015 at 6:39 PM, Chris Angelico wrote: > My crystal ball suggests that defaultdict(list) might be useful here. > > ChrisA I used something similar to this for some problem in hackerrank, anyway i think this is what you want. class defaultlist(object): def __init__(self, factory, data=None): self.factory = factory self.list = [] self.data = data def __getitem__(self, x): if x >= len(self.list): self.list.extend([self.factory() for _ in range(len(self.list), x + 1)]) return self.list[x] def __repr__(self): return str(self) def __str__(self): if len(self.list) == 0: return '(' + str(self.data) + ')[...]' return ''.join(['(', str(self.data), ')['] + map(str, self.list) + [', ...]']) def __setitem__(self, x, v): if x >= len(self.list): self.list.extend([self.factory() for _ in range(len(self.list), x + 1)]) self.list[x] = v def main(): factory = lambda: defaultlist(factory) list_of_lists = defaultlist(factory) print (list_of_lists[0]) list_of_lists[0][10].data = 20 print (list_of_lists[0]) main() Gist: https://gist.github.com/c0c2ee1e7c6535ef8c3d