Break string into list of characters in Python

FlexedCookie picture FlexedCookie · Mar 23, 2012 · Viewed 178.9k times · Source

Essentially I want to suck a line of text from a file, assign the characters to a list, and create a list of all the separate characters in a list -- a list of lists.

At the moment, I've tried this:

fO = open(filename, 'rU')
fL = fO.readlines()

That's all I've got. I don't quite know how to extract the single characters and assign them to a new list.

The line I get from the file will be something like:

fL = 'FHFF HHXH XXXX HFHX'

I want to turn it into this list, with each single character on its own:

['F', 'H', 'F', 'F', 'H', ...]

Answer

Elliot Bonneville picture Elliot Bonneville · Mar 23, 2012

You can do this using list:

new_list = list(fL)

Be aware that any spaces in the line will be included in this list, to the best of my knowledge.