Ask Your Question
2

How can I iterate over a multi-dimensional array in Python using loops?

asked 2023-06-27 11:37:20 +0000

woof gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-06-27 11:45:02 +0000

devzero gravatar image

You can iterate over a multi-dimensional array in Python using nested loops. Here's an example:

my_array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

for i in range(len(my_array)):
    for j in range(len(my_array[i])):
        print(my_array[i][j])

In this example, i represents the row index, and j represents the column index. We use len to determine the sizes of the arrays, and then iterate through each element using the indices. We can then perform any operation we want on each element, such as printing it to the screen.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-27 11:37:20 +0000

Seen: 7 times

Last updated: Jun 27 '23