1

I am trying to split an array of strings into a 2d array of characters from each string

lines = ['abc','123','ggg']
list(lines)
print lines
#['abc','123','ggg']
#nothing changed
#I want [['a','b','c'],['1','2','3'] etc..

Whereas with one string,

print list('lala')
#['l', 'a', 'l', 'a']
#It works!

Thanks

2
  • 1
    On a side note: what's the use case for this? You can use strings exactly like lists, do you really need to convert them? Commented Jan 30, 2012 at 20:28
  • I think you meant 'print list(lines)' Commented Jan 30, 2012 at 20:43

1 Answer 1

7

Using map():

>>> map(list, lines)
[['a', 'b', 'c'], ['1', '2', '3'], ['g', 'g', 'g']]

Or with a list comprehension:

>>> [list(line) for line in lines]
[['a', 'b', 'c'], ['1', '2', '3'], ['g', 'g', 'g']]
Sign up to request clarification or add additional context in comments.

1 Comment

Simple, concise and to the point (maybe add a note, see my comment on the question)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.