Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!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.006 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; '>>>>': 0.09; 'am,': 0.12; '-tkc': 0.16; 'from:addr:python.list': 0.16; 'from:addr:tim.thechases.com': 0.16; 'from:name:tim chase': 0.16; 'message-id:@tim.thechases.com': 0.16; 'received:70.251': 0.16; 'received:dsl.rcsntx.swbell.net': 0.16; 'received:rcsntx.swbell.net': 0.16; 'received:swbell.net': 0.16; 'subject:loops': 0.16; 'wrote:': 0.16; '>>>': 0.18; 'cc:2**0': 0.22; 'header:In-Reply-To:1': 0.22; 'cc:addr:gmail.com': 0.30; 'list': 0.32; 'to:addr:python-list': 0.33; 'difference': 0.34; 'header:User-Agent:1': 0.34; 'reference': 0.35; 'subject:lists': 0.36; 'another': 0.37; 'using': 0.37; 'subject:: ': 0.39; 'to:addr:python.org': 0.39; 'or,': 0.40; 'numbers:': 0.91 Date: Thu, 18 Aug 2011 07:45:10 -0500 From: Tim Chase User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.18) Gecko/20110616 Thunderbird/3.1.11 MIME-Version: 1.0 To: python-list@python.org Subject: Re: lists and for loops References: <8307c651-e4c1-4d28-9f2a-bf0513f21eb8@glegroupsg2000goo.googlegroups.com> In-Reply-To: <8307c651-e4c1-4d28-9f2a-bf0513f21eb8@glegroupsg2000goo.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - boston.accountservergroup.com X-AntiAbuse: Original Domain - python.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tim.thechases.com X-Source: X-Source-Args: X-Source-Dir: Cc: Mark Niemczyk X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 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: 29 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1313671526 news.xs4all.nl 23949 [2001:888:2000:d::a6]:53458 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:11746 On 08/18/2011 07:22 AM, Mark Niemczyk wrote: > Or, using list comprehension. > >>>> numbers = [1, 2, 3, 4, 5] >>>> numbers = [n + 5 for n in numbers] >>>> numbers > [6, 7, 8, 9, 10] Or, if you want it in-place: numbers[:] = [n+5 for n in numbers] which makes a difference if you have another reference to numbers: >>> numbers = [1,2,3,4,5] >>> digits = numbers >>> numbers = [n+5 for n in numbers] >>> numbers, digits ([6, 7, 8, 9, 10], [1, 2, 3, 4, 5]) >>> numbers = [1,2,3,4,5] >>> digits = numbers >>> numbers[:] = [n+5 for n in numbers] >>> numbers, digits ([6, 7, 8, 9, 10], [6, 7, 8, 9, 10]) -tkc