Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #30968 > unrolled thread
| Started by | mooremathewl@gmail.com |
|---|---|
| First post | 2012-10-08 12:28 -0700 |
| Last post | 2012-10-09 14:55 +0200 |
| Articles | 4 on this page of 24 — 16 participants |
Back to article view | Back to comp.lang.python
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
Page 2 of 2 — ← Prev page 1 [2]
| From | alex23 <wuwei23@gmail.com> |
|---|---|
| Date | 2012-10-08 20:11 -0700 |
| Message-ID | <fdad0a35-9f89-4747-af13-aad171cff0bd@wm7g2000pbc.googlegroups.com> |
| In reply to | #30984 |
On Oct 9, 12:06 pm, Roy Smith <r...@panix.com> wrote: > I'm going to go with this one. I think people tend to over-abuse list > comprehensions. I weep whenever I find `_ = [...]` in other people's code.
[toc] | [prev] | [next] | [standalone]
| From | mooremathewl@gmail.com |
|---|---|
| Date | 2012-10-09 07:03 -0700 |
| Message-ID | <53ed3206-456c-4527-9343-48e9a39b690a@googlegroups.com> |
| In reply to | #30984 |
On Monday, October 8, 2012 10:06:50 PM UTC-4, Roy Smith wrote:
> In article <mailman.1976.1349747963.27098.python-list@python.org>,
>
(big snip)
>
>
> > y = list(itertools.chain.from_iterable(('insertme', x[i]) for i in range(len(x))))
>
>
>
> A statement ending in four close parens is usually going to be pretty
>
> difficult to figure out. This is one where I had to pull out my pencil
>
> and start pairing them off manually to figure out how to parse it.
Fair enough. I admit I was looking for a tricky one-liner, which rarely leads to good code...I should know better.
Thanks for all the feedback from everyone. It's amazing how much Python one can learn just asking about a small section of code!
[toc] | [prev] | [next] | [standalone]
| From | Duncan Booth <duncan.booth@invalid.invalid> |
|---|---|
| Date | 2012-10-09 11:40 +0000 |
| Message-ID | <XnsA0E780DF73B31duncanbooth@127.0.0.1> |
| In reply to | #30968 |
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:
>>> x = [1,2,3]
>>> y = ['insertme']*(2*len(x))
>>> y[1::2] = x
>>> y
['insertme', 1, 'insertme', 2, 'insertme', 3]
--
Duncan Booth
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2012-10-09 14:55 +0200 |
| Message-ID | <mailman.1992.1349787318.27098.python-list@python.org> |
| In reply to | #31006 |
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.
[toc] | [prev] | [standalone]
Page 2 of 2 — ← Prev page 1 [2]
Back to top | Article view | comp.lang.python
csiph-web