reverse an array in python

To reverse an array in Python, we can use the built-in reverse() method. Here's an example:

main.py
my_array = [1, 2, 3, 4, 5]
my_array.reverse()
print(my_array)  # Output: [5, 4, 3, 2, 1]
89 chars
4 lines

Alternatively, we can also use slicing to accomplish the same result:

main.py
my_array = [1, 2, 3, 4, 5]
reversed_array = my_array[::-1]
print(reversed_array)  # Output: [5, 4, 3, 2, 1]
108 chars
4 lines

related categories

gistlibby LogSnag