I understand str.join()
:
>>> '|'.join(['1','2','3'])
'1|2|3'
Is there something which outputs a list? Is there a function that will output:
['1', '|', '2','|', '3']
That is, a str.join()
for lists? (or any other iterable?)
list('|'.join(['1','2','3']))
should do the trick where you are working with a list of chars.
A more generic solution, that works for all objects is:
from itertools import izip_longest, chain
def intersperse(myiter, value):
return list(
chain.from_iterable(izip_longest(myiter, [], fillvalue=value))
)[:-1]
I'm not aware of a built-in/std-library version of this function.
In action:
print intersperse([1,2,3], '|')
outputs:
[1, '|', 2, '|', 3]
How about this?
>>> list('|'.join(['1','2','3']))
['1', '|', '2', '|', '3']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With