python 2d array

+2 votes
How to define a 2-D array without an initialized length, something like this: A = [][]
Apr 15, 2018 in Python by ana1504.k
• 7,910 points
1,309 views

4 answers to this question.

+2 votes

You can use NumPy library of Python, below I have mentioned few ways to create 2-D arrays using NumPy:
>>> import numpy
>>> numpy.zeros((5, 5))
array([[ 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.]])
numpy provides a matrix type as well. It's less commonly used, and some people recommend against using it. But it's useful for people coming to numpy from Matlab, and in some other contexts. I thought I'd include it since we're talking about matrices!

>>> numpy.matrix([[1, 2], [3, 4]])
matrix([[1, 2],
[3, 4]])
Here are some other ways to create 2-d arrays and matrices (with output removed for compactness):

numpy.matrix('1 2; 3 4') # use Matlab-style syntax
numpy.arange(25).reshape((5, 5)) # create a 1-d range and reshape
numpy.array(range(25)).reshape((5, 5)) # pass a Python range and reshape
numpy.array([5] * 25).reshape((5, 5)) # pass a Python list and reshape
numpy.empty((5, 5)) # allocate, but don't initialize
numpy.ones((5, 5)) # initialize with ones
numpy.ndarray((5, 5)) # use the low-level constructor

answered Apr 15, 2018 by anto.trigg4
• 3,440 points
+2 votes
>>> import numpy
>>> numpy.zeros((5, 5))
array([[ 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.]])
answered Oct 18, 2018 by rishav
+2 votes
>>> matrix = 5*[5*[0]]
>>> matrix
[[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]]
>>> matrix[4][4] = 2
>>> matrix
[[0, 0, 0, 0, 2], [0, 0, 0, 0, 2], [0, 0, 0, 0, 2], [0, 0, 0, 0, 2], [0, 0, 0, 0, 2]]
answered Oct 18, 2018 by pinre
+2 votes

You should make a list of lists, and the best way is to use nested comprehensions:

>>> matrix = [[0 for i in range(5)] for j in range(5)]
>>> pprint.pprint(matrix)
[[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]]
answered Oct 18, 2018 by ritu

Related Questions In Python

0 votes
1 answer

How do I trim extra zeros from a 2D array in python?

Hey @Laksha, you can try something like ...READ MORE

answered May 29, 2019 in Python by Yamini
2,851 views
0 votes
0 answers

Error in Python script "Expected 2D array, got 1D array instead:"?

I wanted to make this ML prediction: import ...READ MORE

May 18, 2022 in Python by Kichu
• 19,040 points
1,564 views
+1 vote
12 answers
0 votes
1 answer
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,465 views
0 votes
1 answer
+4 votes
7 answers
+2 votes
2 answers

How can I create a new file in Python?

You can try the below code which ...READ MORE

answered Mar 31, 2018 in Python by anto.trigg4
• 3,440 points
1,338 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP