Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #11746

Re: lists and for loops

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 <python.list@tim.thechases.com>
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 <python.list@tim.thechases.com>
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 <a47fc507-4e45-4a1d-b133-10a6bd2095f3@c14g2000yqk.googlegroups.com> <b599dd9d-0533-44ef-9bb5-8ce2db4760e8@m5g2000prh.googlegroups.com> <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 <prahamark@gmail.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.154.1313671526.27778.python-list@python.org> (permalink)
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

Show key headers only | View raw


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


Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

lists and for loops Emily Anne Moravec <moravec@stolaf.edu> - 2011-08-17 20:08 -0700
  Re: lists and for loops alex23 <wuwei23@gmail.com> - 2011-08-17 20:23 -0700
    Re: lists and for loops Mark Niemczyk <prahamark@gmail.com> - 2011-08-18 05:22 -0700
      Re: lists and for loops Tim Chase <python.list@tim.thechases.com> - 2011-08-18 07:45 -0500
  Re: lists and for loops Peter Pearson <ppearson@nowhere.invalid> - 2011-08-18 16:43 +0000

csiph-web