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


Groups > comp.lang.python > #30977

RE: Insert item before each element of a list

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin3!goblin1!goblin.stu.neva.ru!news.astraweb.com!border5.a.newsrouter.astraweb.com!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ramit.prasad@jpmorgan.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'explicitly': 0.04; 'that?': 0.05; 'completeness': 0.07; 'correct.': 0.07; 'nested': 0.07; 'missed': 0.09; '(0,': 0.09; '0),': 0.09; '3),': 0.09; '[0,': 0.09; 'received:155': 0.09; '1),': 0.16; '2),': 0.16; '3)]': 0.16; 'afterwards.': 0.16; 'disclaimers': 0.16; 'disclaimers,': 0.16; 'from:addr:jpmorgan.com': 0.16; 'iterable': 0.16; 'received:155.180': 0.16; 'received:159.53': 0.16; 'received:159.53.110': 0.16; 'received:bankone.net': 0.16; 'received:exchad.jpmchase.net': 0.16; 'received:jpmchase.com': 0.16; 'received:jpmchase.net': 0.16; 'received:svr.bankone.net': 0.16; 'securities,': 0.16; 'subject:item': 0.16; 'tup': 0.16; 'url:disclosures': 0.16; 'url:jpmorgan': 0.16; 'wrote:': 0.17; '>>>': 0.18; 'to:name:python-list@python.org': 0.20; 'tells': 0.22; 'seems': 0.23; 'received:169.254': 0.24; 'tried': 0.25; 'header:In-Reply-To:1': 0.25; 'skip:[ 10': 0.26; 'accuracy': 0.27; 'instead.': 0.27; 'merge': 0.27; 'subject:list': 0.28; 'chris': 0.28; 'fine': 0.28; '>>>>': 0.29; 'though.': 0.29; 'received:169': 0.29; 'header:Received:8': 0.30; 'sense': 0.31; 'to:addr:python- list': 0.33; 'list': 0.35; 'pm,': 0.35; 'but': 0.36; 'wanted': 0.36; 'charset:us-ascii': 0.36; 'why': 0.37; 'item': 0.37; 'subject:: ': 0.38; 'sure': 0.38; 'instead': 0.39; 'to:addr:python.org': 0.39; 'think': 0.40; 'map': 0.61; 'information,': 0.63; 'url:email': 0.63; 'more': 0.63; 'legal': 0.65; 'subject': 0.66; 'purchase': 0.67; 'sale': 0.76; 'absolutely': 0.84; 'received:169.254.8': 0.84; 'subject:before': 0.84
X-DKIM OpenDKIM Filter v2.1.3 sj1.jpmchase.com q98MCpV4011108
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=jpmorgan.com; s=smtpout; t=1349734371; bh=hqeNQFzfL0d6Vl2UCY8GJE7J0lX8TJo29/AV+eqRQII=; h=From:To:Subject:Date:Message-ID:References:In-Reply-To: Content-Transfer-Encoding:MIME-Version:Content-Type; b=dW30kSqRFi7Bbk8k9EFaMUoSQl0+v8QzsNLAMdOPsYJYKZbgLBW3o7tXHktwfEmam UdEfSaJNMjS1j9cGtWmz5sX7Rl41dC6fYzJ0rb8TrSVMLonRG9a1jIGgIT2eSD+hc7 6w4Ze1UH6YI4Z1h8Eopz6BXanuWj0RFt0nUC/z/E=
From "Prasad, Ramit" <ramit.prasad@jpmorgan.com>
To "python-list@python.org" <python-list@python.org>
Subject RE: Insert item before each element of a list
Thread-Topic Insert item before each element of a list
Thread-Index AQHNpYtZle8qHWv1N0ey/Y1jw6exp5ewEvcAgAAHZID//84+UIAASiUA///DdtA=
Date Mon, 8 Oct 2012 22:12:38 +0000
References <fc55bb2e-fdc0-405c-89b5-290aa4fc6c63@googlegroups.com> <CALvWhxuwBbkcW7Gt-W+0FcDDyMePyU3SVBWHu+XpcMUX7dQAFA@mail.gmail.com> <50733399.5040701@freenet.de> <5B80DD153D7D744689F57F4FB69AF474166DFE4C@SCACMX008.exchad.jpmchase.net> <5073480E.6090709@freenet.de>
In-Reply-To <5073480E.6090709@freenet.de>
Accept-Language en-US
Content-Language en-US
X-MS-Has-Attach
X-MS-TNEF-Correlator
x-originating-ip [10.67.79.47]
Content-Transfer-Encoding quoted-printable
MIME-Version 1.0
X-DLP-FWD Yes
Content-Type text/plain; charset="us-ascii"
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.1971.1349734374.27098.python-list@python.org> (permalink)
Lines 23
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1349734374 news.xs4all.nl 6960 [2001:888:2000:d::a6]:46623
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:30977

Show key headers only | View raw


Agon Hajdari wrote:
> On 10/08/2012 11:15 PM, Prasad, Ramit wrote:
> > Agon Hajdari wrote:
> >>
> >> On 10/08/2012 09:45 PM, Chris Kaynor wrote:
> >>> [('insertme', i) for i in x]
> >>
> >> This is not enough, you have to merge it afterwards.
> >
> > Why do you say that? It seems to work just fine for me.
> >
> >>>> x
> > [0, 1, 2, 3, 4]
> >>>> [('insertme', i) for i in x]
> > [('insertme', 0), ('insertme', 1), ('insertme', 2), ('insertme', 3), ('insertme', 4)]
> >
> >>
> >> y = [item for tup in y for item in tup]
> 
> I think he wanted to have a 'plain' list
> a = [0, 1, 0, 2, 0, 3]
> and not
> a = [(0, 1), (0, 2), (0, 3)]

You are absolutely correct. I missed that when I tried it.
Instead of the nested list comprehension, I might have used
map instead.

>>> y = [('insertme', i) for i in x]
>>> z = []
>>> _ = map( z.extend, y )
>>> z
['insertme', 0, 'insertme', 1, 'insertme', 2, 'insertme', 3, 'insertme', 4]

I am not sure which is more Pythonic, but to me map + list.extend tells
me more explicitly that I am dealing with an iterable of iterables.
It might make more sense to only to me though.


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  

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


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