Possible Duplicate:
python: most elegant way to intersperse a list with an element
Assuming I have the following list:
['a','b','c','d','e']
How can I append a new item (in this case a -
) between each item in this list, so that my list will look like the following?
['a','-','b','-','c','-','d','-','e']
Thanks.
The following will add a "separator" element between each of those in a list:
output:
FWIW, here's a similar thread titled Custom string joining in the Usenet
comp.lang.python
group that might interest you.seems to work
Here's a solution that I would expect to be very fast -- I believe all these operations will happen at optimized c speed.
Tested:
or
or
Adapting this answer to a similar question:
Another answer to the same question does this using itertools and a slightly modified separator list: