[Tutor] string to list of characters

Andrei Kulakov ak@silmarill.org
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
> Sean

>>> word = "dog"
>>> l = [letter for letter in word]
>>> l
['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
> http://mail.python.org/mailman/listinfo/tutor

-- 
Cymbaline: intelligent learning mp3 player - python, linux, console.
get it at: http://silmarill.org/cymbaline