[Tutor] Two dimensional lists

Phil phillor9 at gmail.com
Sun Nov 1 20:24:21 EST 2020


I have some C++ code that I wrote some time ago and, just as an 
exercise, I'm converting it to Python. I've come to a standstill because 
of arrays. If I use a numpy array as follows then I can use that array 
in exactly the same way as a C++ array even though I don't understand 
the syntax of it. I used the trial_and_error method to create it.

self.board = numpy.zeros((board_size_x, board_size_y), dtype=numpy.bool)
self.next_board = numpy.zeros((board_size_x, board_size_y), numpy.bool)

Python lists, if I understand this correctly, can be used in the same 
way as arrays. However, I've made little progress. Is there a list 
equivalent to this?

b = [5] , [5] # a square array of size 5

b[3][2] = 9

In Python, b = [[5,5],[5,5]] looked promising but I'm still greeted with 
a list out of range error. I've read through several Python tutorials 
and I've tried append and even though there isn't an error the result 
doesn't make sense.

No doubt the solution is straight forward but this poor old duffer 
cannot see it.

-- 
Regards,
Phil



More information about the Tutor mailing list