Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed5.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'list?': 0.07; 'val': 0.07; 'english,': 0.09; 'lst': 0.09; 'index': 0.13; '7:58': 0.16; 'better?': 0.16; 'entry.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'idx,': 0.16; 'traverse': 0.16; 'wrote:': 0.17; 'replacing': 0.17; 'code.': 0.20; 'bit': 0.21; 'example': 0.23; 'thus': 0.24; 'tried': 0.25; 'header:In-Reply- To:1': 0.25; 'message-id:@mail.gmail.com': 0.27; 'index,': 0.29; "i'm": 0.29; 'fri,': 0.30; 'operate': 0.32; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'list': 0.35; 'nov': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'explain': 0.36; "i'll": 0.36; 'does': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'little': 0.39; 'header:Received:5': 0.40; 'think': 0.40 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:to :content-type; bh=jI6sn9AbAy+u7r1Nj56QlVMA5FOWm+4zrHd2+JDbjng=; b=vWfkp6bZjDSLzDl7dYG2Q3zuU1VexfEnrTYpouXIIj9B7K2gt4QqJ7FKTLncIuk49I 2jjfLWwnvMOBWUHhPnTtQKEi/yWIBfUl8+zBidY40mXafOL+zFJka81DKFX0yeU1FANC /xiSHNWgr0xu2eMVPwGwrLxVp47PFt6ssSteHo3GB6Rbh8pJ8ag3d0FVOTTdo375FzBK ZRSMdx5Jm17wiwpr88Y4JvT91tdxpiJumFEEXfygveXu9aUqc+Nq6Wdh5N/jSJG0Ul5p hqeTsr7nnVplKv3HxHToIiR/EAxBdNhn4zkRWxvkPHwRMFLObOwWr8nB9OBhfhK395iV F3Dw== MIME-Version: 1.0 In-Reply-To: References: Date: Fri, 2 Nov 2012 20:11:49 +1100 Subject: Re: pythonic way From: Chris Angelico To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 22 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351847512 news.xs4all.nl 6891 [2001:888:2000:d::a6]:49987 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32606 On Fri, Nov 2, 2012 at 7:58 PM, jack wrote: > thanks,but I don't think enumerate() is my want > Have some ways to operate the reference of element,not a copy when I tried > to traverse a list? > > I'm so sorry about my poor English, hope you don't mind it. No probs, I'll be a little less vague and pointer-y and give you some example code. :) lst = ['foo', 'bar', 'quux', 'asdf', 'qwer', 'zxcv'] for idx, val in enumerate(lst): if val[1]=='w': lst[idx]='Replaced' print(lst) ['foo', 'bar', 'quux', 'asdf', 'Replaced', 'zxcv'] Does that explain it a bit better? You get the index and can then mutate the list using that index, thus replacing the original entry. ChrisA