1

I would like to create a list of lists from a list.

The list looks like this:

level = ['   WWWWWWWWWWWWWWWWW', 'C  W C             W', 'C  W C             W', 'C  W C             W', 'C  W C             W', 'C  W C             W', 'C  W C             W', 'C  W C             W', 'C  W C             W', 'C  W C             W', 'C  W C             W', 'C  W C             W', 'C                  E']

I need to create this:

[[' ', ' ', ' ', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W', 'W'], ['C', ' ', ' ', 'W', ' ', 'C', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'W'], ['C', ' ', ' ', 'W', ' ', 'C', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'W'], ['C', ' ', ' ', 'W', ' ', 'C', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'W'], ['C', ' ', ' ', 'W', ' ', 'C', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'W'], ['C', ' ', ' ', 'W', ' ', 'C', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'W'], ['C', ' ', ' ', 'W', ' ', 'C', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'W'], ['C', ' ', ' ', 'W', ' ', 'C', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'W'], ['C', ' ', ' ', 'W', ' ', 'C', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'W'], ['C', ' ', ' ', 'W', ' ', 'C', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'W'], ['C', ' ', ' ', 'W', ' ', 'C', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'W'], ['C', ' ', ' ', 'W', ' ', 'C', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'W'], ['C', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'E']]

I have done it this way:

listofLists = []
for row in level:
  liss = []
  for col in row:
    liss.append(col)
  listofLists.append(liss)

What is a more pythonic way or shorter way of doing this?

2 Answers 2

4
>>> listofLists = map(list,level)

(in python3, if you really need a list, do list(map(list, level)))

Sign up to request clarification or add additional context in comments.

1 Comment

What a timing!!! I have to even pass an "I am a human" test to be able to answer.
2

When you call list() on a string this will return the list of all its characters (including spaces).

level = ['   WWWWWWWWWWWWWWWWW', 'C  W C             W', 'C  W C             W', 'C  W C             W', 'C  W C             W', 'C  W C             W', 'C  W C             W', 'C  W C             W', 'C  W C             W', 'C  W C             W', 'C  W C             W', 'C  W C             W', 'C                  E']
trasnsformed = [list(x) for x in level]

Comments

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.