2

I have a long string of characters which I want to split into a list of the individual characters. I want to include the whitespaces as members of the list too. How do I do this?

4
  • 3
    You don't need to do this, since a string is already a sequence of characters. Please provide some example of what you think you're doing. Commented Nov 18, 2010 at 15:45
  • 3
    One difference between between a string and a list of characters is that a list is mutable. Is that why you want the sequence of character converted to that format? In many if not most other respects they are very similar and the same operations can be performed on either (so it might make sense to just leave them alone). Commented Nov 18, 2010 at 16:00
  • @martineau: But mutating a "string as list" isn't really very beneficial, since creating new strings is generally so efficient. It would be helpful to see some actual context around this question rather than guessing. I think it indicates a design problem, but without facts, it's hard to tell what the purpose is. Commented Nov 18, 2010 at 18:31
  • @S.Lott: What you say is uite true, I was just wondering out loud in the hopes that the OP would respond by agreeing or not and directly or indirectly provide some of the needed additional information -- doing what is requested is trivial, the motivation is the more interesting aspect. Commented Nov 18, 2010 at 19:05

3 Answers 3

12

you can do:

list('foo')

spaces will be treated as list members (though not grouped together, but you didn't specify you needed that)

>>> list('foo')
['f', 'o', 'o']
>>> list('f oo')
['f', ' ', 'o', 'o']
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your time and your answers. I think the above answer might help me out.
What I am trying to do is read a string which includes white spaces, from another .txt files. This file contains just "X" or " " (spaces).
I am going to use this to construct a two dimensional map. Since two dimensional arrays are apparently not supported by Python, I have to use nested lists.
One way to simulate a 2D array in Python is to use a dict, with a tuple (x,y) as the key. It's fairly efficient and often easier to manage than nested lists.
@user512316, depending on what you want to do with this, you might want to look into using Numpy.
0

Here some comparision on string and list of strings, and another way to make list out of string explicitely for fun, in real life use list():

b='foo of zoo'
a= [c for c in b]
a[0] = 'b'
print 'Item assignment to list and join:',''.join(a)

try:
    b[0] = 'b'
except TypeError:
    print 'Not mutable string, need to slice:'
    b= 'b'+b[1:]

print b

Comments

0

This isn't an answer to the original question but to your 'how do I use the dict option' (to simulate a 2D array) in comments above:

WIDTH = 5
HEIGHT = 5

# a dict to be used as a 2D array:
grid = {}

# initialize the grid to spaces
for x in range(WIDTH):
    for y in range(HEIGHT):
        grid[ (x,y) ] = ' '

# drop a few Xs
grid[ (1,1) ] = 'X'
grid[ (3,2) ] = 'X' 
grid[ (0,4) ] = 'X'

# iterate over the grid in raster order
for x in range(WIDTH):
    for y in range(HEIGHT):
        if grid[ (x,y) ] == 'X':
            print "X found at %d,%d"%(x,y)

# iterate over the grid in arbitrary order, but once per cell
count = 0
for coord,contents in grid.iteritems():
    if contents == 'X':
        count += 1

print "found %d Xs"%(count)

Tuples, being immutable, make perfectly good dictionary keys. Cells in the grid don't exist until you assign to them, so it's very efficient for sparse arrays if that's your thing.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.