Path: csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed3.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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; '21,': 0.07; 'indexing': 0.07; 'none:': 0.07; 'cc:addr:python-list': 0.11; 'python': 0.11; 'def': 0.12; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'garbage': 0.16; 'item):': 0.16; 'original)': 0.16; 'sequence,': 0.16; 'underlying': 0.16; 'index': 0.16; 'wrote:': 0.18; 'thu,': 0.19; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'raise': 0.29; 'start,': 0.30; 'message-id:@mail.gmail.com': 0.30; 'that.': 0.31; 'easy,': 0.31; 'end,': 0.31; 'class': 0.32; 'subject:time': 0.33; 'skip:_ 10': 0.34; 'offered': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'sequence': 0.36; 'too': 0.37; 'list': 0.37; 'handle': 0.38; 'negative': 0.60; 'range': 0.61; 'more': 0.64; 'details': 0.65; '2015': 0.84; 'hard.': 0.84; '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=MKFCN/RfvMCpwH/LQ1GrwIoVh5Tv9EpUZOJVMYLAaRY=; b=tDh7xMT4c32ra6/7DT0vYrn9g+KHf/uka0mHbS8RKVMxTJ+JpkTB9NLIeVH7OCMXdm KujqH9bf+2DOeNzG4/L0SvrFoHd2C5swj3i1s/+RAWh86XM5q8f1QB1TG28beH99mJ/h +pJvjkmdIOC+rlgPKtp+ZDleGhB1VAav+BLXiTezacj2CF8CQyWDQ5BYm9HfzadLQMl0 Sy2x+7eE7CRgT/0B8rsMJ4rjMS3swI7NXUzWjerWsX5HaDzv0QisqxxMMnV1SOuk9ct0 ot/CvPLJocp63k2bqOi8oPGGwnFq22mgAQX9XGHVqDp4mB30TTsAi8pEnlDFraX6LPNk fEng== MIME-Version: 1.0 X-Received: by 10.43.0.67 with SMTP id nl3mr3736585icb.59.1432154159997; Wed, 20 May 2015 13:35:59 -0700 (PDT) In-Reply-To: References: <9ceklad15llnv3npejq9iuh91soci8aeqo@4ax.com> <555b0621$0$2753$c3e8da3$76491128@news.astraweb.com> <555b6db8$0$12996$c3e8da3$5496439d@news.astraweb.com> Date: Thu, 21 May 2015 06:35:59 +1000 Subject: Re: Slices time complexity From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: , Newsgroups: comp.lang.python Message-ID: Lines: 32 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1432154162 news.xs4all.nl 2831 [2001:888:2000:d::a6]:37655 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:90982 On Thu, May 21, 2015 at 5:51 AM, Mario Figueiredo wrote: > But no one is arguing for that. Instead, it was said that it would be > interesting if Python offered views. It's pretty easy, actually. (Slightly more complicated once you handle more details like negative indexing and strides other than 1, but still not too hard.) class View: def __init__(self, seq, start=0, end=None): self.seq = seq self.start = start if end is None: self.end = len(seq) else: self.end = end def __getitem__(self, item): if isinstance(item, slice): start, end, _ = item.indices(self.end-self.start) return View(self.seq, self.start + start, self.start + end) if self.start + item >= self.end: raise IndexError return self.seq[self.start + item] Start with any sequence (doesn't have to be a list as such) and construct a View of it, and then all slicing and dicing happens with index arithmetic instead of list construction. Indexing of views transparently works on the underlying sequence, and you can coalesce a view into a concrete list (eg to allow garbage collection of the original) with list(v), same as you would with a range object or a generator or any other iterable. It's really not that hard. ChrisA