470,826 Members | 1,811 Online

# Splice two lists

Is there a good way to splice two lists together without resorting to a
manual loop? Say I had 2 lists:

l1 = [a,b,c]
l2 = [1,2,3]

And I want a list:

[a,1,b,2,c,3] as the result.

I've been searching around but I can't seem to find a good example.

Thanks,

Dan McLeran

May 7 '06 #1
6 4559
da********@yahoo.com <da********@yahoo.com> wrote:
Is there a good way to splice two lists together without resorting to a
manual loop? Say I had 2 lists:

l1 = [a,b,c]
l2 = [1,2,3]

And I want a list:

[a,1,b,2,c,3] as the result.

I've been searching around but I can't seem to find a good example.

One way:

result = 6*[None]
result[0::2] = l1
result[1::2] = l2
Alex
May 7 '06 #2
da********@yahoo.com wrote:
Is there a good way to splice two lists together without resorting to a
manual loop? Say I had 2 lists:

l1 = [a,b,c]
l2 = [1,2,3]

And I want a list:

[a,1,b,2,c,3] as the result.

Our good friend itertools can help us out here:
from itertools import chain, izip
x = ['a', 'b', 'c']
y = [1, 2, 3]
list(chain(*izip(x, y))) ['a', 1, 'b', 2, 'c', 3] # You can splice more than two iterables at once too:
z = ['x', 'y', 'z']
list(chain(*izip(x, y, z))) ['a', 1, 'x', 'b', 2, 'y', 'c', 3, 'z'] # Cleaner to define it as a function:
def splice(*its): return list(chain(*izip(*its))) splice(x, y) ['a', 1, 'b', 2, 'c', 3] splice(x, y, z)

['a', 1, 'x', 'b', 2, 'y', 'c', 3, 'z']

--Ben

May 7 '06 #3
"da********@yahoo.com" <da********@yahoo.com> wrote:
Is there a good way to splice two lists together without resorting to a
manual loop? Say I had 2 lists:

l1 = [a,b,c]
l2 = [1,2,3]

And I want a list:

[a,1,b,2,c,3] as the result.

I've been searching around but I can't seem to find a good example.

Here's one possibility:

list(reduce(lambda s, t: s + t, zip(L1, L2), ()))

-M

--
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/ | Dartmouth College, Hanover, NH, USA
May 7 '06 #4
Thanks, this worked great. Can you explain the syntax of the '*' on the
return value of izip? I've only ever seen this syntax with respect to
variable number of args.

Thanks again.

May 7 '06 #5
da********@yahoo.com wrote:
Thanks, this worked great.
Welcome. :-)
Can you explain the syntax of the '*' on the
return value of izip? I've only ever seen this syntax with respect to
variable number of args.

When used in a function call (as opposed to a function definition), *
is the "unpacking" operator. Basically, it "flattens" an iterable into
arguments. The docs mention it...

http://www.python.org/doc/2.4.2/tut/...00000000000000
http://www.python.org/doc/faq/progra...ion-to-another

....but not in great detail. You can apply * to an arbitrary
expression, e.g.:
def f3(a, b, c): pass f3(1, 2, 3)
f3(*range(3))
f3(*[1, 2, 3])

--Ben

May 7 '06 #6
>When used in a function call (as opposed to a function definition), *
is the "unpacking" operator. Basically, it "flattens" an iterable into
arguments. The docs mention it...

Cool, looks like I didn't read carefully enough.

Thanks again.

May 7 '06 #7

### This discussion thread is closed

Replies have been disabled for this discussion.