[Tutor] string to list of characters
Tue, 07 Aug 2001 23:36:20 -0400
On Wed, Aug 08, 2001 at 04:29:22AM +0100, sean dowie wrote:
> I want to turn a word into a list of the letters, eg "dog" becomes ['d',
> 'o', 'g'].
> Is there a simple way to use split() or something to do this, or do I have
> to loop through each letter of the string?
> Thanks in advance
>>> word = "dog"
>>> l = [letter for letter in word]
['d', 'o', 'g']
That's list comprehension.. It's described in detail in the tutorial.
It's kind of neat.
> Tutor maillist - Tutor@python.org
Cymbaline: intelligent learning mp3 player - python, linux, console.
get it at: http://silmarill.org/cymbaline