 ## Python Program: Reverse a list of numbers

In this article, you will learn to Reverse a list of numbers in Python in various ways to such as reverse() method, for loop, def function, etc.

Let’s see another Python project.

### Using Python Slicing

Example: Using Python Slicing.

```				```
number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(number[10:1:-1])
```
```

Explanation:

A list called a number is defined with elements from 1 to 10.

The print() function is used to display the result. Inside the print() function, the number[10:1:-1] is evaluated.

List slicing in Python uses the syntax [start:end:step] to select a portion of a list. In this case, the start index is 10, the end index is 1, and the step is -1.

Since the start index is greater than the end index and the step is negative, the selected portion of the list will be in reverse order. The range starts at index 10 (inclusive) and goes up to index 2 (exclusive), moving backward with a step of -1. The value at index 1 is not included in the result.

Eaxmple:

```				```
number = [1, 2, 3]
print(number[::-1])
```
```

Explanation:

A list called a number is defined with three elements: 1, 2, and 3.

The print() function is used to display the result. Inside the print() function, the number[::-1] is evaluated.

The [::] colon at the start and end position in slicing represents that start and end indices are omitted, and the entire list is selected. The step is -1, indicating that the list should be traversed in reverse order.

The list [1, 2, 3] is reversed, and the resulting list [3, 2, 1] is printed.

### Using reverse method

Example: Using Python built-in reverse method

```				```
number = [1, 2, 3]
number.reverse()
print(number)
```
```

Explanation:

A list called a number is defined with elements from 1 to 3.

Now, the Python built-in reverse() method is applied to the list “number”.

Inside the print() function, the ‘number’ is evaluated that displays a reverse list of elements.

### Using for loop

Example: Using for loop

```				```
num = [1, 2, 3]
for number in reversed(num):
print(number)
```
```

Explanation:

A list called a num is defined with elements from 1 to 3.

The reversed() method reverses the list of numbers and stores in the variable “number” during for loop iteration over each number sequentially one by one and displays the number using the print() function.

Example:

```				```
num = [1, 2, 3]
reversed_list = num.copy()
reversed_list.reverse()

print("Original List:",num)
print("Reversed List:", reversed_list)
```
```

Explanation:

A list called a num is defined with elements from 1 to 3.

Copied the original list using copy() function and stored it in the variable reversed_list.

Now, used the reverse() method on the variable reversed_list and reversed the list of numbers.

Using the print() function, displayed both lists; original and reversed list.

### Using def function

Example: Using def function

```				```
def reverse_list(num):
num.reverse()
print(num)
reverse_list([1,2,3])
```
```

Explanation:

Defined a function reverse_list(num) and passed a parameter num to it.

Inside the function, a reverse() method is applied to the variable num to reverse the list of numbers and displayed using the print() function.

Finally, call the function reverse_list() and passed an argument as [1,2,3] to the parameter num.

### Using def function with Python Slicing

Example: Using def function with Python Slicing.

```				```
num = [1,2,3]
def reverse_list():
print(num[::-2])
reverse_list()
```
```

Explanation:

A list called a num is defined with elements from 1 to 3.

Defined a function reverse_list().

Inside the function, implemented Python slicing on print(num[::-2]), meaning that the [::] colon at the start and end position in slicing represents that start and end indices are omitted, and the entire list is selected. The step is -2, indicating that the list should be traversed in reverse order.

### Conclusion

The article presents a step-by-step process for reversing a list. It explains the logic behind the algorithm, which involves iterating through the list and swapping elements from the front and back. This approach ensures that all elements are correctly reversed in place.

Overall, the article on reversing a list of numbers successfully addresses the problem and provides a well-explained solution.

Recent Articles