How do you use two-dimensional arrays in Python?

Asked 2 years ago, Updated 2 years ago, 62 views

Without specifying a size for a two-dimensional array Matrix = [][] I want to write it like this, but an error appears

Matrix = [5][5] I keep getting errors even if I choose the size How do you use two-dimensional arrays in Python?

python array

2022-09-22 22:23

1 Answers

To write a two-dimensional array, initialize the outer list first You need to put the item in the outside list.

# Create a 5x5 list with all elements zero
Matrix = [[0]*5 for i in range(5)]

# Change the value by accessing the index
Matrix[0][0] = 1
Matrix[4][0] = 5

#Output
print Matrix[0][0] # prints 1
print Matrix[4][0] # prints 5


2022-09-22 22:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.