Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.033 X-Spam-Evidence: '*H*': 0.93; '*S*': 0.00; 'skip:[ 20': 0.04; 'insert': 0.05; 'cc:addr:python-list': 0.11; '[none,': 0.16; 'none]': 0.16; 'skip:w 80': 0.16; 'to:addr:web.de': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'cc:addr:python.org': 0.22; '>>>': 0.24; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; '>': 0.26; 'equivalent': 0.26; 'header:In-Reply-To:1': 0.27; 'words': 0.29; 'subject:list': 0.30; 'message- id:@mail.gmail.com': 0.30; 'url:mailman': 0.30; 'url:python': 0.33; 'third': 0.33; 'subject:the': 0.34; "can't": 0.35; 'received:google.com': 0.35; 'url:listinfo': 0.36; 'subject:?': 0.36; 'url:org': 0.36; 'skip:& 10': 0.38; 'skip:[ 10': 0.38; 'moving': 0.39; 'url:mail': 0.40; 'how': 0.40; 'length': 0.61; 'side': 0.67; 'skip:w 30': 0.69; 'power': 0.76; 'as:': 0.81; 'otten': 0.84 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 :cc:content-type; bh=kRWsQ6Jdi+GDpQ00lJ3SM976XEd/lPDWNxMdbWjmkcU=; b=C9Svw7Li7HznNKxANfvNSO8De5MI0YrOfOUoLPKAl2DLeYy8WDDYCIYSHP2uNyeT7w MUMZ9D7WDZ0ao4azH4vqyyjOgLeZFvw7i95GEPoQ/a1eFSrI4jZBYUrmW1Tr13lz5L6O +iFFYu8fTFKqm+LmFdst38vKSE4posYvow1WWVicuhY4+sCp0yyOB2pTvRFL5aupSfPA fkQ34Or7Vq7T0xVqMvb8XlogGRw+IHaOM/GdbIqq5ShdRv5FX2qm7+iUQpq3iiQtTn0h mp+GSD4aDpcDMKYSQ+0WN5+X4NuBi2n3AfX5idlhRsjH8qLRk3S0hbUlk89luShOjRFm 0zOw== MIME-Version: 1.0 X-Received: by 10.112.64.8 with SMTP id k8mr16241lbs.72.1397048977104; Wed, 09 Apr 2014 06:09:37 -0700 (PDT) In-Reply-To: References: Date: Wed, 9 Apr 2014 21:09:37 +0800 Subject: Re: how to insert the elements in a list properly? From: length power To: Peter Otten <__peter__@web.de> Content-Type: multipart/alternative; boundary=001a11c3ee7803a23604f69bd08c X-Mailman-Approved-At: Wed, 09 Apr 2014 15:39:48 +0200 Cc: python-list@python.org 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: 97 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1397050789 news.xs4all.nl 2941 [2001:888:2000:d::a6]:44215 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:69947 --001a11c3ee7803a23604f69bd08c Content-Type: text/plain; charset=ISO-8859-1 words = ["x1", "x2", "x3", "x4", "x5"] words.append(words.pop(2)) words.append(words.pop(2)) words ['x1', 'x2', 'x5', 'x3', 'x4'] why i can't write it as: [words.append(words.pop(2)) for i in range(0,2)] >>> [words.append(words.pop(2)) for i in range(0,2)] [None, None] 2014-04-09 18:46 GMT+08:00 Peter Otten <__peter__@web.de>: > length power wrote: > > > word=["x1","x2","x3","x4","x5"] > > w=word[-2:] > > del word[-2:] > > word.insert(2,w) > > word > > ['x1', 'x2', ['x4', 'x5'], 'x3'] > > > > what i want to get is > > ['x1', 'x2', 'x4', 'x5', 'x3'] > > > > how can i insert them ? > > Make the left-hand side an empty slice: > > >>> words = ["x1", "x2", "x3", "x4", "x5"] > >>> w = words[-2:] > >>> del words[-2:] > >>> words[2:2] = w > >>> words > ['x1', 'x2', 'x4', 'x5', 'x3'] > > Or note that the operation is equivalent to moving the third item to the > end: > > >>> words = ["x1", "x2", "x3", "x4", "x5"] > >>> words.append(words.pop(2)) > >>> words > ['x1', 'x2', 'x4', 'x5', 'x3'] > > > -- > https://mail.python.org/mailman/listinfo/python-list > --001a11c3ee7803a23604f69bd08c Content-Type: text/html; charset=ISO-8859-1
words = ["x1", "x2", "x3", "x4", "x5"]
words.append(words.pop(2))
words.append(words.pop(2))
words
['x1', 'x2', 'x5', 'x3', 'x4']
why i can't write it as:

[words.append(words.pop(2)) for i in range(0,2)]

>>> [words.append(words.pop(2)) for i in range(0,2)]
[None, None]


2014-04-09 18:46 GMT+08:00 Peter Otten <__peter__@web.de>:
length power wrote:

> word=["x1","x2","x3","x4","x5"]
> w=word[-2:]
> del word[-2:]
> word.insert(2,w)
> word
> ['x1', 'x2', ['x4', 'x5'], 'x3']
>
> what i want to get is
> ['x1', 'x2', 'x4', 'x5', 'x3']
>
> how can i insert them ?

Make the left-hand side an empty slice:

>>> words = ["x1", "x2", "x3", "x4", "x5"]
>>> w = words[-2:]
>>> del words[-2:]
>>> words[2:2] = w
>>> words
['x1', 'x2', 'x4', 'x5', 'x3']

Or note that the operation is equivalent to moving the third item to the
end:

>>> words = ["x1", "x2", "x3", "x4", "x5"]
>>> words.append(words.pop(2))
>>> words
['x1', 'x2', 'x4', 'x5', 'x3']


--
https://mail.python.org/mailman/listinfo/python-list

--001a11c3ee7803a23604f69bd08c--