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

Ned Batchelder ned at nedbatchelder.com
Tue Oct 31 12:41:42 EDT 2017


On 10/31/17 12:29 PM, Stefan Ram wrote:
> Ned Batchelder <ned at nedbatchelder.com> writes:
>> However you solve it, do yourself a favor and write a function to
>> encapsulate it:
>    It is always a good solution to encapsulate a pattern into
>    a function. So I agree that this is a good suggestion. But
>    just for the sole sake of information, I'd like to add that
>    this is also the slowest solution so far (about 10.76 usec).
>
>    This might be a case where macros would be fine. As readable
>    as a function call, but no runtime-overhead. One can write
>
> value_list =  [1, 2, 3, 4, 56, 's']
>
> #define JOIN_WRAPPED(list,string) \
> string + string.join(map(str,list)) + string
>
> values = JOIN_WRAPPED(value_list,'||')
>
> print( values )
>
>    and save it as »source.c« and execute it using
>
> gcc -E source.c -o source.py
> python source.py
>
>    . This is also not intended to be a recommendation.
>

I try to avoid micro-optimization.  My guess is that right after calling 
wrapped_join(), the result will be written to an I/O device of some 
kind.  If that is so, the time spent in wrapped_join will be irrelevant.

--Ned.



More information about the Python-list mailing list