To reverse a list without using the reverse()
method in Python, you can use the slicing operator. You need to use the [::-1]
slicing operator on any list that you want to reverse.
The [::-1]
slicing operator starts from the end of the list towards its beginning in a step of -1 which results in a reversed copy of the original list.
See this example:
numbers = [1, 2, 3, 4, 5] # Reverse the list reversed_numbers = numbers[::-1] print(reversed_numbers) # Output: 👉 [5, 4, 3, 2, 1]
As you can see, we got a reversed copy of the original list in just a single line of code. But how does it work? Let me explain.
The basic syntax of slicing in Python is given as follows:
sequence[start:stop:step]
In this syntax:
sequence
– It is any valid sequence in Python such as a list, tuple, string, etc.start
– The index of the first element you want to include in the slice.stop
– The index of the first element you don’t want to include in the slice.step
– The step size to use when traversing the sequence.
Let me also break down start
, stop
and step
:
Start Index
:- If not specified, it defaults to the beginning of the sequence.
- If negative, it counts from the end of the sequence. For example,
-1
refers to the last element.
Stop Index
:- If not specified, it defaults to the end of the sequence.
- If negative, it counts from the end of the sequence.
Step Size
:- If not specified, it defaults to
1
, meaning every element. - If negative, it reverses the order of elements in the slice.
- If not specified, it defaults to
Let’s take an example of a basic slicing where we have a list with 10 items and we want to slice it from start index 2, to end index 7. Please note that the item at index 7 will not be included in the sliced list.
# Original list sequence = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # Slice the list result = sequence[2:7] print(result) # Output: 👉 [2, 3, 4, 5, 6]
In this example, we specified only the starting and ending index for slicing and did not provide any value for the step size. Therefore, its default value 1 is used.
So, if we do not provide the value of any parameter i.e. start
, stop
and step
, their default values will be used in the slicing.
This means the slicing operator [::]
will give us the exact copy of the original list:
sequence = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # Slice the original list result = sequence[::] print(result) # Output: 👉 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
This means if we keep the default values of the start
and stop
indexes but specify the step size as -1, we will get a reversed copy of the original list. That’s what we did in the very first example.
See this example:
sequence = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] # Slice the original list result = sequence[::-1] print(result) # Output: 👉 [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
You can also use this technique to reverse other sequences such as strings, tuples, etc.
2. Reverse a List using a For Loop
You can also use a regular for loop to create a reversed copy of the original list. This approach is useful when you don’t want to rely on any built-in method and instead want to build your own custom logic.
In this approach, we loop through each item of the original list and append each item to the beginning of the reversed list.
See this example:
# Original list numbers = [1, 2, 3, 4, 5] # Create an empty list reversed_numbers = [] for num in numbers: # Append each item at the beginning reversed_numbers = [num] + reversed_numbers print(reversed_numbers) # Output: 👉 [5, 4, 3, 2, 1]
That’s all for this article. Thanks for reading.