[Tutor] Strange join syntax

Scot Stevenson scot@possum.in-berlin.de
Fri, 26 Apr 2002 20:03:59 +0200


Hello Remco, 

While talking about list reverse, you gave an example that included

> drow = ''.join(wordlist)

I tried this construction myself, and I see that it works, but I'm not 
sure if I understand what is happening here. My docs [Python 2.1.1, yes 
I'll be updating when I have time to install SuSE 8.0] say:

=================
join(words[, sep]) 
Concatenate a list or tuple of words with intervening occurrences of sep. 
The default value for sep is a single space character. It is always true 
that "string.join(string.split(s, sep), sep)" equals s. 
=================

So I would have expected 

>>> wordlist = list('alabaster')
>>> wordlist
['a', 'l', 'a', 'b', 'a', 's', 't', 'e', 'r']
>>> newlist = string.join(wordlist, '')
>>> newlist
'alabaster'

Is there any special reason why you didn't do it this way? 

I'm amazed that the Python Elves know what do to with the '' at the 
beginning, but then I guess that's just magic for you =8).

Thanks, 
Y, Scot