How to join elements at the beginning and end of the list

Neil Cerutti neilc at norwich.edu
Tue Oct 31 11:29:14 EDT 2017


On 2017-10-31, Ganesh Pal <ganesh1pal at gmail.com> wrote:
> Here is my solution
>
>>>> values = '||' + '||'.join(map(str, value_list)) + '||'
>>>> values
>
> '||1||2||3||4||56||s||'
>
> I am joining the elements at the beginning and end of the list
> using '+' operator any other solution, this is not looking
> neater
>
> I am a Linux user using python 2.7

You can use the % operator instead of +, and a generator
expression instead of map. It's a pretty small improvement,
though.

values = '||%s||' % ('||'.join(str(s) for s in value_list))

At least... I THINK you can use that generator expression in 2.7.

-- 
Neil Cerutti




More information about the Python-list mailing list