loop a two dimensional array and create a new one with numbers over 10 in python

One way to loop through a two-dimensional array and create a new one with numbers over 10 is to use nested loops and conditional statements. Here's an example code:

main.py
# original two-dimensional array
original_array = [[4, 8, 15], [16, 23, 42], [5, 9, 11]]

# new two-dimensional array
new_array = []

# loop through each row
for row in original_array:
    # create a new list for the row
    new_row = []
    # loop through each element in the row
    for element in row:
        # check if the element is over 10
        if element > 10:
            # add element to new row
            new_row.append(element)
        else:
            # add 0 to new row
            new_row.append(0)
    # add new row to new array
    new_array.append(new_row)

# print original and new arrays
print("Original array:\n", original_array)
print("New array:\n", new_array)
690 chars
26 lines

In this example, we first define the original two-dimensional array. Then, we define an empty new array. We use a nested loop to loop through each row in the original array and each element in each row. If an element is greater than 10, we add it to the new row. Otherwise, we add 0 to the new row. Finally, we add the new row to the new array. We print both the original and new arrays at the end.

gistlibby LogSnag