1

I want to create for example n matrices like:

m1 = [[0,0],[0,0]]
m2 = [[0,0],[0,0]]
.
. 
mn = [[0,0],[0,0]]
asked Jul 22, 2015 at 17:28
3
  • possible duplicate of How to create nested lists in python? Commented Jul 22, 2015 at 17:31
  • res = [[item for item in range(2)] for item in range(10)] Commented Jul 22, 2015 at 17:31
  • That'll create lists of [0, 1], not [0, 0]. It also has the wrong dimensions (10x2 instead of nx2x2). Commented Jul 22, 2015 at 17:36

1 Answer 1

2

I think that will work for you

res = [[[0 for item3 in range(2)] for item2 in range(2)] for item1 in range(10)]
print res

Output:

[
 [[0, 0], [0, 0]],
 [[0, 0], [0, 0]],
 [[0, 0], [0, 0]],
 [[0, 0], [0, 0]],
 [[0, 0], [0, 0]],
 [[0, 0], [0, 0]],
 [[0, 0], [0, 0]],
 [[0, 0], [0, 0]],
 [[0, 0], [0, 0]],
 [[0, 0], [0, 0]]
]

Basically 10(your n value) arrays with 2x2 lists of zeros.

answered Jul 22, 2015 at 17:42
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.