Python Reverse List with Examples

Posted in /   /  

Python Reverse List with Examples
vinaykhatri

Vinay Khatri
Last updated on November 24, 2024

    A Python list can contain multiple items, and each piece is mapped using index values. The index value of the Python list starts from 0 up to n-1 (n is the total number of elements present in the list). In this Python tutorial, we will discuss how you can reverse a Python list. So, let's get started!

    How to Reverse a Python List?

    There are primarily four ways that we can use to reverse a Python list, and they are as follows:

    • List reverse() method.
    • Python reversed() built-in function.
    • Using List Slicing to reverse a list.
    • Using code logic or algorithms to reverse a list.

    1. Python List reverse() method

    Python list has many built-in methods, and reverse() is one of them. The reverse() method reverses the elements of a list without creating a new list. It does not return any value, instead, it operates on the actual list.

    Example:

    my_list= [10,20,30,40,50]
    
    my_list.reverse() 
    
    print(my_list)

    Output

    [50, 40, 30, 20, 10]

    2. Python reversed() method

    reversed() is a Python built-in function that accepts an iterable object, such as list, string, dictionary, and tuple, and returns a reversed object or iterator. The reversed iterator contains the element in the reversed order. We can use the reversed() function to reverse a list.

    Example

    my_list=[10,20,30,40,50]
    reversed_list = reversed(my_list)
    print(list(reversed_list))

    Output

    [50, 40, 30, 20, 10]

    3. Reverse Python list using List Slicing

    In Python, we have a unique list feature called list slicing. By using list slicing, we can print a sequence of elements from a list. Also, this feature can also be used to reverse a list. List slicing does not change the list element position, it is just used to display or return a sequence of elements.

    List slicing Syntax

    list_name[starting index : final index : steps]

    If we leave the starting index and final index value blank and pass the step value as -1, the list can be displayed or returned in reverse order. This method works because Python lists support negative index, and -1 represent the last index.

    Example

    my_list=[10,20,30,40,50]
    reversed_list = my_list[ : : -1]
    print(reversed_list)

    Output

    [50, 40, 30, 20, 10]

    4. Use Code and Logic to Create a Function to Reverse a Python List

    There are various algorithms that can help to reverse a list.

    Example 1

    Loop through the last index to the 0th index of a list and save every element in a new list as shown below:

    def rev_lis(l):
        rev =[]
        size = len(l)
    
        #loop from the last index value of list
        for i in range(size-1 , -1, -1):
            rev.append(l[i])
    
        return rev
    
    my_list = [10,20,30,40,50]
    reversed_list = rev_lis(my_list)
    
    print(reversed_list)

    Output

    [50, 40, 30, 20, 10]

    Example 2

    Loop to the middle of the list and swap the values of left indexes with the corresponding right indexes as shown below:

    my_list = [10,20,30,40,50]
    size= len(my_list)-1
    
    for i in range(size//2):
        #swap the left values with right
        my_list[i],my_list[size-i]= my_list[size-i],my_list[i]
    
    print(my_list)

    Output

    [50, 40, 30, 20, 10]

    Takeaway

    Following are the key points that you need to remember for reversing a Python list with the methods described in this tutorial:

    • Python list has a method called reverse() that can quickly reverse the list elements.
    • reverse() method return None.
    • reverse() method reverse the existing list.
    • Python reversed() function accepts an iterable object and returns a reversed iterator.
    • To reverse a list using list slicing, pass the value of steps as -1.

    People are also reading:

    Leave a Comment on this Post

    0 Comments