gistlib
To reverse an array in Python, we can use the built-in reverse() method. Here's an example:
reverse()
main.pymy_array = [1, 2, 3, 4, 5] my_array.reverse() print(my_array) # Output: [5, 4, 3, 2, 1] 89 chars4 lines
my_array = [1, 2, 3, 4, 5] my_array.reverse() print(my_array) # Output: [5, 4, 3, 2, 1]
Alternatively, we can also use slicing to accomplish the same result:
main.pymy_array = [1, 2, 3, 4, 5] reversed_array = my_array[::-1] print(reversed_array) # Output: [5, 4, 3, 2, 1] 108 chars4 lines
my_array = [1, 2, 3, 4, 5] reversed_array = my_array[::-1] print(reversed_array) # Output: [5, 4, 3, 2, 1]
gistlibby LogSnag