Path: csiph.com!usenet.pasdenom.info!gegeweb.org!newsfeed0.kamp.net!newsfeed.kamp.net!feeder1.cambriumusenet.nl!feed.tweaknews.nl!194.109.133.83.MISMATCH!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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; 'python,': 0.02; 'cpython': 0.05; 'modified': 0.07; '[0,': 0.09; 'arrays': 0.09; 'linear': 0.09; 'slices': 0.09; 'snippet': 0.09; 'python': 0.11; '2.7': 0.14; 'constructs': 0.16; 'deleted,': 0.16; 'it),': 0.16; 'otoh,': 0.16; 'replaced.': 0.16; 'semantics': 0.16; 'shared.': 0.16; 'slice.': 0.16; 'underlying': 0.16; 'wrote:': 0.18; 'looked': 0.18; 'work,': 0.20; '>>>': 0.22; 'basis,': 0.24; 'url:moin': 0.24; 'mon,': 0.24; '(or': 0.24; 'header:In-Reply-To:1': 0.27; 'array': 0.29; 'message-id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; 'url:wiki': 0.31; 'constant': 0.31; 'piece': 0.31; 'reflected': 0.31; 'lists': 0.32; 'languages': 0.32; 'run': 0.32; 'url:python': 0.33; 'subject:time': 0.33; "i'd": 0.34; 'could': 0.34; 'something': 0.35; 'anybody': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'possible': 0.36; 'url:org': 0.36; 'list': 0.37; 'list.': 0.37; 'being': 0.38; 'implement': 0.38; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'expect': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'information': 0.63; '2015': 0.84; '3.4': 0.84; 'different.': 0.84; 'mirrors': 0.84; 'pertains': 0.84; 'faced': 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 :content-type; bh=4TSDJOF6v5ITThiAPIT+CBV5vM4CrbiUZIYn8hCY4X8=; b=eDodq+Tgx/PIUCHzuUp2FzVPcd6poAYx7nqJ9PNk8xluiP2F15kA5N0b+1rZjFf1WV a2KcptzPgCNW3z51FsHfNnxFW7G/SiB62++MGHNX3oV/7tr9bO3DiqTClJpiWxQn3UQY S8aZIrFMzuHFTTQOAUR4zWbuDoRvfGFMgK71vNxTHu7LUXQynLmBC1ltek1Zocq4j61N KTuRR3AAPc/OA6tckQliPmkWBE05hvq5GJQ7zYwXPwJmJxR6c/LLPvaa/BE7lhmQVHfj nPgrpm7tDrats4QBKVRQhIGmVJgElSFCOgpG8hM5xwzmMux+KcwinON7fOh7g7A3258H Jj9w== X-Received: by 10.107.136.89 with SMTP id k86mr29933631iod.63.1431978625518; Mon, 18 May 2015 12:50:25 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <9ceklad15llnv3npejq9iuh91soci8aeqo@4ax.com> References: <9ceklad15llnv3npejq9iuh91soci8aeqo@4ax.com> From: Ian Kelly Date: Mon, 18 May 2015 13:49:45 -0600 Subject: Re: Slices time complexity To: Python 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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1431978628 news.xs4all.nl 2825 [2001:888:2000:d::a6]:32926 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:90811 On Mon, May 18, 2015 at 1:23 PM, Mario Figueiredo wrote: > I'd like to understand what I'm being told about slices in > https://wiki.python.org/moin/TimeComplexity > > Particularly, what's a 'del slice' and a 'set slice' and whether this > information pertains to both CPython 2.7 and 3.4. "Del Slice" is the operation where a slice of a list is deleted, and "Set Slice" is the operation where a slice is replaced. E.g.: >>> x = list(range(100)) >>> del x[2:98] >>> x [0, 1, 98, 99] >>> x[1:3] = [7, 6, 5, 4, 3] >>> x [0, 7, 6, 5, 4, 3, 99] > Other languages implement slices. I'm currently being faced with a Go > snippet that mirrors the exact code above and it does run in linear > time. > > Is there any reason why Python 3.4 implementation of slices cannot be > a near constant operation? The semantics are different. IIRC, a slice in Go is just a view of some underlying array; if you change the array (or some other slice of it), the change will be reflected in the slice. A slice of a list in Python, OTOH, constructs a completely independent list. It may be possible that lists in CPython could be made to share their internal arrays with other lists on a copy-on-write basis, which could allow slicing to be O(1) as long as neither list is modified while the array is being shared. I expect this would be a substantial piece of work, and I don't know if it's something that anybody has looked into.