Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ian Kelly Newsgroups: comp.lang.python Subject: Re: Drowning in a teacup? Date: Fri, 1 Apr 2016 14:52:10 -0600 Lines: 27 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de 7pPgIaH794I7PozOyi2cVQyEPyd6UL1AiwOrkyWIxltg== 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; 'python,': 0.02; 'received:209.85.223': 0.03; 'removes': 0.05; 'inserts': 0.07; 'line:': 0.07; 'methods,': 0.09; 'slow.': 0.09; 'instead.': 0.15; '2016': 0.16; 'assignment.': 0.16; 'mylist': 0.16; 'nope,': 0.16; 'overwriting': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'variable': 0.18; 'header:In-Reply-To:1': 0.24; 'fri,': 0.27; 'parameters': 0.27; 'message-id:@mail.gmail.com': 0.27; 'function': 0.28; 'problem': 0.33; 'previous': 0.34; 'lists': 0.34; 'that,': 0.34; 'list': 0.34; 'received:google.com': 0.35; 'something': 0.35; 'problem.': 0.35; 'but': 0.36; 'should': 0.36; 'instead': 0.36; 'received:209.85': 0.36; 'to:addr:python- list': 0.36; 'subject:?': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'thought': 0.37; 'received:209': 0.38; 'to:addr:python.org': 0.40; 'still': 0.40; 'called': 0.40; 'your': 0.60; 'contrary': 0.72; 'as:': 0.79; 'front.': 0.84; 'length;': 0.84; 'to:name:python': 0.84; 'wrong!': 0.84 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; bh=RzEVH7wkyMM25GkcdkXnzykjDHk6banQPWSnwJVEGs4=; b=WxrF9pFj6G5KfUPD3nukvxLx8DOysh5DDaYqHGi4GYJB7aLCSuMMpsjqgwB9vYJZnB aRBT5Pb8nQmTvp04H7KJdK6xK/Vhfw6746EJo38xmbx8QpagxR7JbrldFLcvyTfx0Lk4 P+LXKn71O5GidCcTTN6cZ0oNU7CR74v/GdJUH8ucLO8vCmW42yrbqQ48LVB5wb0OByRS EhQN+HmqI5IrZLOnnhlUqTW9mq3dFxiMGptFvZNx8h184tjB1EsKTZJK1PB2C+42ltzM pma2VRhOyeQabIInnaBDSVLKRliXmD4RN9iw56xNQ3uCHwxaryUwWmd0f7pZ0iwVhsRl p1Bg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=RzEVH7wkyMM25GkcdkXnzykjDHk6banQPWSnwJVEGs4=; b=DtfPao6SAAlf9heCmMgl8TIhb4V1OukCkX094Aj5NYKmL9QBYbGK+JFjbhqYoCIQpD OgUqACbAsBDAkSKUALScR/AWtLxtWTi3k2p+XAOrPQJywqj6lXD5SRPdXVSy10VFriz9 LDRVVo3DxZifHjJXwREUfnqxryhJo1pmPY7VaIMTYK5B1Mzz65dj5Z1QID8AbUJM+8Yl AiAlYRBC5oUu0gFTObb1VcQyVZP4IfAyPgX9X52cNFrAd0Euf8aIfEzmhMNzLU39gHDE +N07T4YBhfYtP6uXy8I0FGrGL9jWpYYJW7dMydD66m0DKwhqzvVpU2rn/CxFrRohbffW SOfg== X-Gm-Message-State: AD7BkJLQ2N/H+1tEHttKrKmlFB5TcFeQBdUY9IuTvU15kS4mv5RA93Nqcg0FVya+88A+OGuZCF4DRUrJzIJ3LQ== X-Received: by 10.107.19.157 with SMTP id 29mr7875633iot.11.1459543969829; Fri, 01 Apr 2016 13:52:49 -0700 (PDT) In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21 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:106263 On Fri, Apr 1, 2016 at 2:39 PM, Rob Gaddi wrote: > Fillmore wrote: >> Nope, wrong! contrary to what I thought I had understood about how >> parameters are passed in Python, the function is acting on a copy(!) and >> my original list is unchanged. >> > > Nope, that's not your problem. Your problem is the line: > >> mylist = [mylist[i]] + mylist[:i] + mylist[i+1:] > > Which should be read as: "Take mylist[i], all of mylist before i, > and all of mylist after i and create a new list from it. Store that > new list in a variable called mylist, overwriting the previous value." > > If instead you were using the .insert and .remove methods, you'd be > manipulating the existing list instead. But you don't want to do that, > because those methods are O(N) on the list length; manipulating the > middle of lists is slow. Or use slice assignment. This should work in place of the above: mylist[:] = [mylist[i]] + mylist[:i] + mylist[i+1:] Still O(n), but so will be any implementation that removes something from an arbitrary list position and inserts it at the front.