0
0
PythonProgramBeginner · 2 min read

Python Program to Reverse an Array

You can reverse an array in Python using reversed_array = array[::-1] or array.reverse() to reverse it in place.
📋

Examples

Input[1, 2, 3, 4, 5]
Output[5, 4, 3, 2, 1]
Input[10, 20, 30]
Output[30, 20, 10]
Input[]
Output[]
🧠

How to Think About It

To reverse an array, think of flipping the order of elements so the last becomes first and the first becomes last. You can do this by swapping elements from the start and end moving towards the center, or by creating a new array that reads the original from end to start.
📐

Algorithm

1
Get the input array.
2
Create a new array or modify the original to reverse the order of elements.
3
If creating a new array, start from the last element and add elements to the new array until the first is reached.
4
If modifying in place, swap the first and last elements, then move inward until the middle is reached.
5
Return or print the reversed array.
💻

Code

python
array = [1, 2, 3, 4, 5]
reversed_array = array[::-1]
print(reversed_array)
Output
[5, 4, 3, 2, 1]
🔍

Dry Run

Let's trace the example array [1, 2, 3, 4, 5] through the code that uses slicing to reverse it.

1

Original array

array = [1, 2, 3, 4, 5]

2

Apply slicing to reverse

reversed_array = array[::-1] results in [5, 4, 3, 2, 1]

3

Print reversed array

Output: [5, 4, 3, 2, 1]

StepArray State
Initial[1, 2, 3, 4, 5]
After slicing[5, 4, 3, 2, 1]
💡

Why This Works

Step 1: Using slicing

The array[::-1] syntax creates a new array by reading the original array from the end to the start.

Step 2: In-place reversal

Alternatively, array.reverse() changes the original array by swapping elements from both ends moving inward.

Step 3: Output

Printing the reversed array shows the elements in the opposite order from the original.

🔄

Alternative Approaches

Using reverse() method
python
array = [1, 2, 3, 4, 5]
array.reverse()
print(array)
This reverses the array in place without creating a new array.
Using reversed() function
python
array = [1, 2, 3, 4, 5]
reversed_array = list(reversed(array))
print(reversed_array)
This returns an iterator which we convert to a list for the reversed array.

Complexity: O(n) time, O(n) or O(1) space

Time Complexity

Reversing requires visiting each element once, so it takes linear time proportional to the array size.

Space Complexity

Using slicing or reversed() creates a new array, so space is O(n). Using reverse() modifies in place, so space is O(1).

Which Approach is Fastest?

In-place reversal with reverse() is fastest in space, but slicing is often simpler and more readable.

ApproachTimeSpaceBest For
Slicing (array[::-1])O(n)O(n)When you want a new reversed array
In-place reverse() methodO(n)O(1)When you want to reverse the original array without extra space
reversed() functionO(n)O(n)When you want an iterator or a new reversed list
💡
Use slicing array[::-1] for a quick and easy way to reverse an array without changing the original.
⚠️
Beginners often forget that array.reverse() changes the original array and returns None, so printing its result directly shows None.