Opposite of split

Steven Howe howe.steven at gmail.com
Sun Aug 15 15:10:10 EDT 2010


  On 08/15/2010 11:35 AM, Gary Herron wrote:
> On 08/15/2010 11:24 AM, Alex van der Spek wrote:
>> Looking for a method that does the opposite of 'split', i.e. elements 
>> in a list are automatically concatenated with a user selectable 
>> spacer in between e.g. '\t'. This is to prepare lines to be written 
>> to a sequential file by 'write'.
>>
>> All hints welcome.
>>
>> Regards,
>> Alex van der Spek
>
> Strings have a join method for this:
>     '\t'.join(someList)
>
> Gary Herron
or maybe:
-----------------------------------------
res = ""
for item in myList:
     res = "%s\t%s" %  ( res, item )

myList = ["abc","def","hjk"]
res = ""
for item in myList:
     res = "%s\t%s" % ( res, item )
res
'\tabc\tdef\thjk'


print res
     abc    def    hjk

Note the leading tab.
-----------------------------------------
So:
 >>> res.strip()
'abc\tdef\thjk'
 >>> print res.strip()
abc    def    hjk

simple enough. Strange you had to ask.

sph




More information about the Python-list mailing list