Javaexercise.com

Reverse A Python List elements with or without using built-in functions

In this article, we will write a python program to reverse a list in python.

We know that the list is an ordered set of values enclosed in square brackets [ ].

We can access the elements in the list by their particular index. So our task is to reverse a list containing the elements.

Let's understand the topic with the help of some simple programming examples.

Sample input and output examples:

Input : list = [6, 5, 4, 3, 2, 1]
Output : [1, 2, 3, 4, 5, 6]
​
Input : list = [15, 35, 63, 27, 74, 24]
Output : [24, 74, 27, 63, 35, 15]

We will use the following approaches to solve this problem:

  1. By using the reversed() built-in function

  2. By using the reverse() function

  3. By using list indexing

  4. By using the slice() function

  5. By using a for loop

Let's understand the topic with the help of some simple programs.

Reverse a list by using the reversed() function in Python

Python has a built-in function called reversed(), which takes an iterable and returns the reverse version of it.

This method works similarly to the list indexing method, which we are going to see later in this article.

Algorithm:

Step 1: Start

Step 2: Create a list

Step 3: Use reversed() function on the list

Step 4: Use the list() function to create a list out of the iterator object

Step 5: Print the reversed list

Step 6: Stop

Look at the program below to understand the implementation of the above-mentioned algorithm.

Here, note that reversed() function returns an iterator object and we need to use the list() function to create a list out of the iterator object.

# Take a list
original_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print("The Original list is ",original_list)

# Reversed a list using reversed() function
reversed_list = list(reversed(original_list))

# Display the new list
print("The reversed list is: ", reversed_list)

Output:

The Original list is  [1, 2, 3, 4, 5, 6, 7, 8, 9]
The reversed list is:  [9, 8, 7, 6, 5, 4, 3, 2, 1]

Reverse a list by using the reverse() function in Python

Similar to the above method, python has a method reverse() function that acts directly on the list. Here, while using this method there is no need to assign the old list to a new list.

It helps when we want to reverse a list in place.

Algorithm:

Step 1: Start

Step 2: Create a list

Step 3: Use the reverse() function on that list

Step 4: Print the reversed list

Step 5: Stop

See the code below to understand the above algorithm clearly.

# Take a list
original_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print("The Original list is ",original_list)

# Reversing a list using reverse() function
original_list.reverse()

# Display the new list
print("The reversed list is: ", original_list)

Output:

The Original list is [1, 2, 3, 4, 5, 6, 7, 8, 9]

The reversed list is: [9, 8, 7, 6, 5, 4, 3, 2, 1]

Reverse a list by using the list indexing in Python

When we do indexing of a list, many people think of accessing all items between a start and an end position.

In python, we can use step that allows us to move across indices at different rates.

For example, if we use the list[ : :1 ] then this will return the entire list from beginning to end. If we use the list[ : : -1] then it will traverse the list from the last item to the first.

This is the technique, we used in this program. It is also called slicing in python.

Algorithm:

Step 1: Start

Step 2: Create a list

Step 3: Use the list indexing method on the list

Step 4: Print the reversed the list

Step 5: Stop

See the code below to understand how we can use the list indexing method.

# Take a list
original_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print("The Original list is ",original_list)

# Reverse a Python list with list indexing
reversed_list = original_list[::-1]

# Display the new list
print("The reversed list is: ", original_list)

Output:

The Original list is [1, 2, 3, 4, 5, 6, 7, 8, 9]

The reversed list is: [9, 8, 7, 6, 5, 4, 3, 2, 1]

Reverse a list by using the slice() function in Python

Python has a built-in function called slice() that allows us to index an object in the same way that the list indexing method works.

Algorithm:

Step 1: Start

Step 2: Create a list

Step 3: Use the slice method on the list

Step 4: Print the reversed list

Step 5: Stop

# Take a list
original_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print("The Original list is ",original_list)

# Reverse a Python list with slice()
reversed_list = original_list[slice(None, None, -1)]

# Display the new list
print("The reversed list is: ", reversed_list)

Output:

The Original list is [1, 2, 3, 4, 5, 6, 7, 8, 9]

The reversed list is: [9, 8, 7, 6, 5, 4, 3, 2, 1]

Reverse a list by using a for loop in Python

Here, we will use the reversed() and a for loop in combination to reverse the list. It is helpful when we want to reverse a list without any function.

Algorithm:

Step 1: Start

Step 2: Create a list having items

Step 3: Create an empty list

Step 4: Use for loop with reversed()

Step 5: Append all the elements to the empty list

Step 5: Print that list after appending

Step 6: Stop

See the python program below which is the implementation of the above algorithm.

# Reverse a Python list with a for loop
original_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
print("original list", original_list)

reversed_list = []

for element in reversed(original_list):
   reversed_list.append(element)

print("The reversed list is: ", reversed_list)

Output:

original list [1, 2, 3, 4, 5, 6, 7, 8, 9]
The reversed list is:  [9, 8, 7, 6, 5, 4, 3, 2, 1]