• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / Python / Reverse a List in Python Without Reverse Function

Reverse a List in Python Without Reverse Function

February 15, 2022 Leave a Comment

In Python, there are many ways we can reverse a list. While we can use the list reverse() function, there are other ways to reverse a list in Python without the reverse() function.

The easiest way to reverse a list in Python without using reverse() is with slicing.

list = [1,2,3,4]

list = list[::-1]

print(list)

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

You can also use recursion to reverse a list in Python.

list = [1,2,3,4]

def reverse_list(list):
    if len(list) == 1:
        return list
    return reverse_list(list[1:]) + list[0:1]

print(reverse_list(list))

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

You can also use a for loop to swap the items in the list, starting with swapping the first and last items, then the second and second to last items, and so on and so forth.

list = [1, 2, 3, 4]

for i in range(int(len(list)/2)):
    item_at_i = list[i]
    list[i] = list[len(list) - i - 1]
    list[len(list) - i - 1] = item_at_i 

print(list)

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

When using list variables in Python, we can easily perform list manipulation to change the values or order of the list variables.

One such manipulation is to reverse a list. In Python, there is a built-in function called reverse(), but there are other ways we can reverse a list without the reverse() function.

The easiest way to reverse a list in Python without using the reverse() function is with slicing.

Below is an example in Python of how to reverse a list without the use of the reverse() function.

list = [1,2,3,4]

list = list[::-1]

print(list)

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

Reverse a List Without reverse() Function in Python Using Recursion

Another way we can reverse a list in Python without the reverse() function is with a recursive function.

For recursion, we need to define a base case and a recursive step.

The base case for our recursive reverse function is when our list has a length of one. The recursive step keeps slicing the list from the second element to the end and add the first element to the end.

Below is an example of how to use recursion to reverse a list in Python.

list = [1,2,3,4]

def reverse_list(list):
    if len(list) == 1:
        return list
    return reverse_list(list[1:]) + list[0:1]

print(reverse_list(list))

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

Reverse a List in Python Without reverse() Function Using For Loop

We can also use a for loop to reverse a list in Python.

To use a for loop for reversing a list without the reverse() function, we will swap items in the list in the following way. First, we swap the first and last items. Next, we continue with swapping the second and second to last items, then the third and third to last items, until we reach the middle of the list.

Below is an example of how to use a loop to reverse a list in Python.

list = [1, 2, 3, 4]

for i in range(int(len(list)/2)):
    item_at_i = list[i]
    list[i] = list[len(list) - i - 1]
    list[len(list) - i - 1] = item_at_i 

print(list)

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

Hopefully this article has been useful for you to learn how to reverse a list without the reverse() function in Python.

Other Articles You'll Also Like:

  • 1.  Squaring in Python – Square a Number Using Python math.pow() Function
  • 2.  Truncate String in Python with String Slicing
  • 3.  Perform Reverse Dictionary Lookup in Python
  • 4.  Difference Between read(), readline() and readlines() in Python
  • 5.  Python Random Uniform – Generate Random Numbers Uniformly in Range
  • 6.  pandas max – Find Maximum Value of Series or DataFrame
  • 7.  Python Check if Object is Iterable with hasattr() Function
  • 8.  Factorial Program in Python Using For Loop and While Loop
  • 9.  Using Matplotlib and Seaborn to Create Pie Chart in Python
  • 10.  pandas percentile – Calculate Percentiles of Series or Columns in DataFrame

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy