Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Insert item before each element of a list
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Ian Kelly  
View profile  
 More options Oct 8 2012, 3:43 pm
Newsgroups: comp.lang.python
From: Ian Kelly <ian.g.ke...@gmail.com>
Date: Mon, 8 Oct 2012 13:42:08 -0600
Local: Mon, Oct 8 2012 3:42 pm
Subject: Re: Insert item before each element of a list

On Mon, Oct 8, 2012 at 1:28 PM,  <mooremath...@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.

Using the "roundrobin" recipe from the itertools documentation:

x = [1, 2, 3]
y = list(roundrobin(itertools.repeat('insertme', len(x)), x))


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.