multidimensional lists

Gerhard Häring gh at ghaering.de
Tue Oct 7 12:59:02 EDT 2003


Orla O'Sullivan wrote:
> I have a list of characters and I want to convert it
> into a list of lists..how can I do this??

Unless you want to change the caracters, I can't think of a good reason 
why you would need this.

Anyway, here we go for the simplest way:

 >>> l = ["abc", "xyz"]
 >>> [list(x) for x in l]
[['a', 'b', 'c'], ['x', 'y', 'z']]

It's called a "list comprehension". As you're probably a Python newbie 
you might want to see a less concise version that's easier to understand:

 >>> l2 = []
 >>> for s in l:
...     l2.append(list(s))
...
 >>> print l2
[['a', 'b', 'c'], ['x', 'y', 'z']]


-- Gerhard





More information about the Python-list mailing list