Function/method returning list of chars in string?

John Machin sjmachin at lexicon.net
Tue Jun 9 09:50:06 CEST 2009


Hendrik van Rooyen <mail <at> microcorp.co.za> writes:

> 
> One can go from lb = ['b','a','n','a','n','a']
> to s = "banana" by using s = "".join(lb)
> 
> Is there a way to go the reverse route?

| >>> list('Hendrik')
['H', 'e', 'n', 'd', 'r', 'i', 'k']

Bonus extras: try tuple() and set()

> I have not been able to find one.
> 
> It is obviously easy to write a for char in s loop
> or list comprehension,

Aha! try a functional equivalent of a list comp:

| >>> map(None, 'Hendrik')
['H', 'e', 'n', 'd', 'r', 'i', 'k']

> using lb = s.split("") would have been nice
> as a complement to s = "".join(lb).

object_of_type_X(something) is nicer IMHO.

> Split is already full of magic, it could do with more.

Both str.split and re.split are /too/ full of magic IMHO.







More information about the Python-list mailing list