how do i change from string to list
dataangel
k04jg02 at kzoo.edu
Sun Oct 24 15:22:48 EDT 2004
Michael Hoffman wrote:
> Ivo Woltring wrote:
>
>> str='spot'
>>
>> lst = [] # declare the list first
>> for letter in str:
>> lst.append(letter) # append to the list
>>
>> print lst
>
>
> I just would have used list("spot").
>
>> result: ['s', 'p', 'o', 't']
>
>
> Same result but faster, more transparent, and with a lot less code.
>
> Also, please don't top-post.
Also, if for some reason you didn't want to use list that would look
more elegant as a list comprehension:
>>> y = "Hello!"
>>> [x for x in y]
['H', 'e', 'l', 'l', 'o', '!']
>>>
More information about the Python-list
mailing list