Concatenate list values

wxjmfauth at gmail.com wxjmfauth at gmail.com
Tue Feb 24 05:30:45 EST 2015


Le lundi 23 février 2015 18:15:11 UTC+1, Mark Lawrence a écrit :
> On 23/02/2015 16:14, Tim Chase wrote:
> > On 2015-02-23 07:58, loial wrote:
> >> Is there a quick way to concatenate all the values in a list into a
> >> string, except the first value?
> >>
> >> I want this to work with variable length lists.
> >>
> >> All values in list will be strings.
> >
> > Using
> >
> >    "".join(my_list[1:])
> >
> > should work.
> >
> > If it is an arbitrary iterable instead of a list (and thus can't be
> > sliced), you can use
> >
> >    "".join(itertools.islice(my_iter, 1, None))
> >
> > -tkc
> >
> 
> Presumably rather more efficient for long lists as you're not taking a 
> copy of (effectively) the entire list.  Any volunteers to measure it, 
> not mentioning any names, Mr D'Aprano? :)
> 
> -- 

>>> timeit.repeat("'a'.join(['a']*123)")
[2.747081951123846, 2.7309830094075096, 2.7286731458373197]
>>> timeit.repeat("'\u1234'.join(['a']*123)")
[2.7187153298930298, 2.7063901499382155, 2.709908310428318]
>>> 


>>> timeit.repeat("'a'.join(['a']*123)")
[2.935619986680422, 2.931890334845548, 2.9307190587571768]
>>> timeit.repeat("'\u1234'.join(['a']*123)")
[6.233187127305428, 6.218316962117797, 6.228552434296944]
>>>

What is wrong by design will always stays wrong.
(On top of this, it's buggy)

jmf




More information about the Python-list mailing list