Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: MRAB Newsgroups: comp.lang.python Subject: Re: Drowning in a teacup? Date: Fri, 1 Apr 2016 21:45:02 +0100 Lines: 61 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de WIIwcNCe8OYIw3AnyORmhgjdZFJ6eFDnrPhybvQsCqNw== 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; 'matches': 0.07; 'strings.': 0.07; 'way:': 0.09; 'python': 0.10; 'itself.': 0.11; 'def': 0.13; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'instead).': 0.16; 'key):': 0.16; 'keyword,': 0.16; 'message- id:@mrabarnett.plus.com': 0.16; 'mylist': 0.16; 'nope,': 0.16; 'passed.': 0.16; 'proceeding': 0.16; 'received:192.168.1.4': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; 'element': 0.18; 'passes': 0.18; 'refers': 0.18; 'pass': 0.22; 'elements': 0.23; 'replacing': 0.23; 'skip:b 30': 0.24; 'header :In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'order.': 0.27; 'parameters': 0.27; 'object,': 0.27; 'function': 0.28; "i'm": 0.30; 'code': 0.30; '(i.e.,': 0.30; 'fixed': 0.31; 'anyone': 0.32; 'another': 0.32; 'right?': 0.33; 'list': 0.34; 'but': 0.36; 'beginning': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'say': 0.37; 'thought': 0.37; 'list.': 0.37; 'doing': 0.38; 'front': 0.38; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'called': 0.40; 'skip:u 10': 0.61; 'here.': 0.62; 'binding': 0.66; 'contrary': 0.72; 'snap': 0.84; 'wrong!': 0.84; 'notorious': 0.91 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=K//fZHiI c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=L9H7d07YOLsA:10 a=9cW_t1CCXrUA:10 a=s5jvgZ67dGcA:10 a=IkcTkHD0fZMA:10 a=c0-da2HE1KEK-CwPlLQA:9 a=QEXdDO2ut3YA:10 X-AUTH: mrabarnett@:2500 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.7.1 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:106261 On 2016-04-01 21:27, Fillmore wrote: > > notorious pass by reference vs pass by value biting me in the backside > here. Proceeding in order. > > I need to scan a list of strings. If one of the elements matches the > beginning of a search keyword, that element needs to snap to the front > of the list. > I achieved that this way: > > > for i in range(len(mylist)): > if(mylist[i].startswith(key)): > mylist = [mylist[i]] + mylist[:i] + mylist[i+1:] > > Since I need this code in multiple places, I placed it inside a function > > def bringOrderStringToFront(mylist, key): > > for i in range(len(mylist)): > if(mylist[i].startswith(key)): > mylist = [mylist[i]] + mylist[:i] + mylist[i+1:] > > and called it this way: > > if orderstring: > bringOrderStringToFront(Tokens, orderstring) > > right? > 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. > > I fixed it this way: > > def bringOrderStringToFront(mylist, key): > > for i in range(len(mylist)): > if(mylist[i].startswith(key)): > mylist = [mylist[i]] + mylist[:i] + mylist[i+1:] > return(mylist) > > and: > > if orderstring: > Tokens = bringOrderStringToFront(Tokens, orderstring) > > but I'm left with a sour taste of not understanding what I was doing > wrong. Can anyone elaborate? what's the pythonista way to do it right? > Python always passes a reference to the object, so the name "mylist" in the function is a local name that refers to the list that you passed. When you say "mylist = something", you're just binding that local name to another object (i.e., it'll now refer to that object instead). What you want to do it mutate the list itself. You can do that by replacing its elements with the new list you've created: mylist[:] = [mylist[i]] + mylist[:i] + mylist[i+1:]