Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #31013
| Path | csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| 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; 'skip:[ 20': 0.03; '[1,': 0.09; 'booth': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'feedback.': 0.15; 'gmane': 0.16; 'itertools': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'slicing:': 0.16; 'subject:item': 0.16; 'url:general': 0.16; 'url:gmane': 0.16; 'url:thread': 0.16; 'wrote:': 0.17; 'proposed': 0.20; 'suggested': 0.20; 'import': 0.21; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.28; 'this?': 0.28; 'subject:list': 0.28; 'post': 0.28; '>>>>': 0.29; 'surprised': 0.29; "i'm": 0.29; 'usually': 0.30; 'url:python': 0.32; 'skip:l 40': 0.33; 'to:addr:python-list': 0.33; 'list': 0.35; 'there': 0.35; 'list.': 0.35; 'received:org': 0.36; 'url:org': 0.36; "didn't": 0.36; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'email addr:gmail.com': 0.63; 'gut': 0.84; 'subject:before': 0.84; 'url:focus': 0.84; 'faster.': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: Insert item before each element of a list |
| Date | Tue, 09 Oct 2012 14:55:20 +0200 |
| Organization | None |
| References | <fc55bb2e-fdc0-405c-89b5-290aa4fc6c63@googlegroups.com> <XnsA0E780DF73B31duncanbooth@127.0.0.1> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p5084a243.dip.t-dialin.net |
| User-Agent | KNode/4.7.3 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| 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.1992.1349787318.27098.python-list@python.org> (permalink) |
| Lines | 33 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1349787318 news.xs4all.nl 6869 [2001:888:2000:d::a6]:59183 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:31013 |
Show key headers only | View raw
Duncan Booth wrote:
> mooremathewl@gmail.com wrote:
>
>> What's the best way to accomplish this? Am I over-complicating it?
>> My gut feeling is there is a better way than the following:
>>
>>>>> import itertools
>>>>> x = [1, 2, 3]
>>>>> y = list(itertools.chain.from_iterable(('insertme', x[i]) for i in
>>>>> range(len(x)))) y
>> ['insertme', 1, 'insertme', 2, 'insertme', 3]
>>
>> I appreciate any and all feedback.
>>
>
> Given the myriad of proposed solutions, I'm surprised nobody has suggested
> good old list slicing:
My post on gmane
http://thread.gmane.org/gmane.comp.python.general/718940/focus=718947
apparently didn't make it through to the list.
>>>> x = [1,2,3]
>>>> y = ['insertme']*(2*len(x))
>>>> y[1::2] = x
>>>> y
> ['insertme', 1, 'insertme', 2, 'insertme', 3]
An advantage of this approach -- it is usually much faster.
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Insert item before each element of a list mooremathewl@gmail.com - 2012-10-08 12:28 -0700
Re: Insert item before each element of a list Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-08 13:42 -0600
Re: Insert item before each element of a list MRAB <python@mrabarnett.plus.com> - 2012-10-08 20:43 +0100
Re: Insert item before each element of a list Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-08 14:04 -0600
Re: Insert item before each element of a list Agon Hajdari <agonh@freenet.de> - 2012-10-08 22:12 +0200
Re: Insert item before each element of a list Peter Otten <__peter__@web.de> - 2012-10-08 23:12 +0200
RE: Insert item before each element of a list "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-10-08 21:15 +0000
Re: Insert item before each element of a list Agon Hajdari <agonh@freenet.de> - 2012-10-08 23:39 +0200
RE: Insert item before each element of a list "Prasad, Ramit" <ramit.prasad@jpmorgan.com> - 2012-10-08 22:12 +0000
Re: Insert item before each element of a list Paul Rubin <no.email@nospam.invalid> - 2012-10-08 15:24 -0700
Re: Insert item before each element of a list Nobody <nobody@nowhere.com> - 2012-10-08 23:35 +0100
Re: Insert item before each element of a list "Alex" <foo@email.invalid> - 2012-10-09 00:08 +0000
Re: Insert item before each element of a list Terry Reedy <tjreedy@udel.edu> - 2012-10-08 21:58 -0400
Re: Insert item before each element of a list Roy Smith <roy@panix.com> - 2012-10-08 22:06 -0400
Re: Insert item before each element of a list rusi <rustompmody@gmail.com> - 2012-10-08 19:34 -0700
Re: Insert item before each element of a list rusi <rustompmody@gmail.com> - 2012-10-08 19:39 -0700
Re: Insert item before each element of a list Hans Mulder <hansmu@xs4all.nl> - 2012-10-12 00:21 +0200
Re: Insert item before each element of a list Terry Reedy <tjreedy@udel.edu> - 2012-10-11 19:38 -0400
Re: Insert item before each element of a list Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-12 02:16 +0000
Re: Insert item before each element of a list Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-09 12:01 +0000
Re: Insert item before each element of a list alex23 <wuwei23@gmail.com> - 2012-10-08 20:11 -0700
Re: Insert item before each element of a list mooremathewl@gmail.com - 2012-10-09 07:03 -0700
Re: Insert item before each element of a list Duncan Booth <duncan.booth@invalid.invalid> - 2012-10-09 11:40 +0000
Re: Insert item before each element of a list Peter Otten <__peter__@web.de> - 2012-10-09 14:55 +0200
csiph-web