Program to reverse the list using Python

Introduction

In this section of python programming, we will understand the different ways to reverse the elements in the list.

Program to reverse the list using Python

Program

list = [1, 15, 13, 20, 14, 18, 9]

list1 = [1, 15, 13, 20, 14, 18, 9]

# Using reverse()

def reverse_list(list):

    list.reverse()

    return list

print("Using reverse()",reverse_list(list))

#Using reversed

def reversed_list(list1):

    return [i for i in reversed(list1)]

print("Using reversed()",reversed_list(list1))

Output

Program to reverse the list using Python Output

Explanation

In the above code, we have used reverse() and reversed() function to reverse the elements in the list. These two are python in-built function which easily reverse the list and provide us the output. In addition to this, we can also use slicing technique to reverse. But the drawback of using slicing is that copying the list requires more memory.

Author

  • Barry Allen

    A Full Stack Developer with 10+ years of experience in different domain including SAP, Blockchain, AI and Web Development.

    View all posts

Comments

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.