Splice two lists

D

danmcleran

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
 
A

Alex Martelli

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
 
M

Michael J. Fromberger

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
 
B

Ben Cartwright

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
 
D

danmcleran

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.
 
B

Ben Cartwright

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/node6.html#SECTION006740000000000000000
http://www.python.org/doc/faq/progr...yword-parameters-from-one-function-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
 
D

danmcleran

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,294
Messages
2,571,511
Members
48,206
Latest member
EpifaniaMc

Latest Threads

Top