[Tutor] list concatenation
Gonçalo Rodrigues
op73418@mail.telepac.pt
Wed Feb 5 16:26:02 2003
----- Original Message -----
From: "Lance" <lbrannma@cablespeed.com>
To: "Tutor" <tutor@python.org>
Sent: Wednesday, February 05, 2003 3:37 PM
Subject: [Tutor] list concatenation
> Hi All,
>
> What's the fastest way to concatenate the same string to all elements of a
> list? I have a list of filenames and want to concatentate the path, a
common
> path, to each of them.
>
There aren't many choices really. Supposing lst is your list and pathname is
the path to concatenate, use the os.path module as in:
import os
#Avoid recurrent name resolution.
joinpath = os.path.join
#Change list in place => avoid allocation of new list.
for i in range(len(lst)):
lst[i] = joinpath(lst[i], pathname)
If you need to keep the old list lst a list comprehension will do, as in:
newlist = [joinpath(path, pathname) for path in lst]
> Thanks,
> Lance
>
Hope it helps,
G. Rodrigues