Concatenate list values

Peter Otten __peter__ at web.de
Mon Feb 23 11:09:10 EST 2015


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.
> 
> Any help appreciated

>>> strings
['All', 'values', 'in', 'list', 'will', 'be', 'strings']

Build a new list without the first item:

>>> strings[1:]
['values', 'in', 'list', 'will', 'be', 'strings']

Concatenate the strings:

>>> "|".join(strings[1:])
'values|in|list|will|be|strings'

If you don't want a separator invoke the join method on an empty string:

>>> "".join(strings[1:])
'valuesinlistwillbestrings'





More information about the Python-list mailing list