How to concatenate list members
Daniel Klein
danielk at aracnet.com
Thu May 30 08:40:53 EDT 2002
On Thu, 30 May 2002 14:02:38 +0200, "Ruediger Maehl"
<ruediger.maehl_nospam at web.de> wrote:
>Hello Pythoneers,
>
>I would like to concatenate all members of a list of strings.
>How can I do that?
>I know, I can use a for loop over all elements and concatenate
>them to one string. But is there another more efficient solution?
>
>Rüdiger
>
># ===========================================
>def concat1(alist, sep=" "):
> res = ""
> for e in alist:
> res += str(e)
> res += sep
>
> return res[:-1]
>
>a = ["200100002", "200100003", "200100004"] # many many more to come ...
>
>b = concat1(a, "_")
>
>print b
>
># and b then looks like this:
># "200100002_200100003_200100004"
Try this:
def concat1(alist,sep=" "):
return sep.join(alist)
I think you need at least Python2.0 for this. Otherwise change it to:
import string
def concat1(alist, sep=" "):
return string.join(alist,sep)
Daniel Klein
More information about the Python-list
mailing list