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

Neil Cerutti neilc at norwich.edu
Tue Oct 31 12:12:35 EDT 2017


On 2017-10-31, Stefan Ram <ram at zedat.fu-berlin.de> wrote:
> Neil Cerutti <neilc at norwich.edu> writes:
>>You can use the % operator instead of +, and a generator
>>expression instead of map. It's a pretty small improvement,
>>though.
>
>   "Improvement" in what sense?
>
> C:\>python -m timeit -s "value_list =  [1, 2, 3, 4, 56, 's']" "values = '||' + '||'.join(map(str, value_list)) + '||'"
> 100000 loops, best of 3: 4.86 usec per loop
>
> C:\>python -m timeit -s "value_list =  [1, 2, 3, 4, 56, 's']" "values = '||%s||' % ('||'.join(str(s) for s in value_list))"
> 100000 loops, best of 3: 7.46 usec per loop

Hmmm.... minty freshness?

-- 
Neil Cerutti




More information about the Python-list mailing list