How to concatenate list members

Ruediger Maehl ruediger.maehl_nospam at web.de
Thu May 30 08:02:38 EDT 2002


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"







More information about the Python-list mailing list