[Tutor] Splitting a string into a list.
Danny Yoo
dyoo@hkn.eecs.berkeley.edu
Wed, 24 Jul 2002 19:02:11 -0700 (PDT)
On Thu, 25 Jul 2002, Troels Leth Petersen wrote:
> Isn't there a built-in that does the following:
>
> >>> liste = []
> >>> for char in thing:
> ... liste.append(char)
> ...
> >>> liste
> ['l', 'o', 't', 't', 'o']
###
>>> lotto = "youareawinner"
>>> list(lotto)
['y', 'o', 'u', 'a', 'r', 'e', 'a', 'w', 'i', 'n', 'n', 'e', 'r']
###
Yes. *grin*
The list() builtin will turn any sequence-like thing into a list of its
elements. To go back, from a list of characters to a string, is a little
weirder: we can use a join, using the empty string:
###
>>> ''.join(list(lotto))
'youareawinner'
###
Hope this helps!