{"id":24720,"date":"2021-11-02T09:45:44","date_gmt":"2021-11-02T04:15:44","guid":{"rendered":"https:\/\/python-programs.com\/?p=24720"},"modified":"2021-11-05T17:22:35","modified_gmt":"2021-11-05T11:52:35","slug":"python-list-reverse-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-list-reverse-method-with-examples\/","title":{"rendered":"Python List reverse() Method with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python List remove() Method with Examples<\/a> Lists in Python are mutable sequences. They are extremely similar to tuples, except they do not have immutability constraints. Lists are often used to store collections of homogeneous things, but there is nothing stopping you from storing collections of heterogeneous items as well.<\/p>\n List reverse() Method in Python:<\/strong><\/p>\n The reverse() method reverses the list’s elements.<\/p>\n Syntax:<\/strong><\/p>\n Parameters: <\/strong>This function has no parameters.<\/p>\n Return Value:<\/strong><\/p>\n The reverse() method produces no output. It is used to update the existing list.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Covered all examples on python concepts and missed checking out the Python List Method Examples<\/a> then go with the available link & gain more knowledge about List method concepts in python.<\/p>\n In the previous article, we have discussed Python List remove() Method with Examples List in Python: Lists in Python are mutable sequences. They are extremely similar to tuples, except they do not have immutability constraints. Lists are often used to store collections of homogeneous things, but there is nothing stopping you from storing collections of …<\/p>\n
\nList in Python:<\/strong><\/p>\nlist.reverse()<\/pre>\n
Given List = ['welcome', 'to', 'Python', 'programs']<\/pre>\n
The given original list: ['welcome', 'to', 'Python', 'programs']\r\nThe given list after reversing : ['programs', 'Python', 'to', 'welcome']<\/pre>\n
Given List = [10, 20, 30, 40, 50]<\/pre>\n
The given original list: [10, 20, 30, 40, 50]\r\nThe given list after reversing : [50, 40, 30, 20, 10]<\/pre>\n
List reverse() Method with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = [\"welcome\", \"to\", \"Python\", \"programs\"]\r\n# Print the given original list.\r\nprint(\"The given original list:\", gvn_lst)\r\n# Apply reverse() function to the given list that reverses all the elements of\r\n# the given list.\r\ngvn_lst.reverse()\r\n# Print the list after reversing all its elements.\r\nprint(\"The given list after reversing : \", gvn_lst)\r\n<\/pre>\n
The given original list: ['welcome', 'to', 'Python', 'programs']\r\nThe given list after reversing : ['programs', 'Python', 'to', 'welcome']<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Print the given original list.\r\nprint(\"The given original list:\", gvn_lst)\r\n# Apply reverse() function to the given list that reverses all the elements of\r\n# the given list.\r\ngvn_lst.reverse()\r\n# Print the list after reversing all its elements.\r\nprint(\"The given list after reversing : \", gvn_lst)\r\n\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 10 20 30 40 50\r\nThe given original list: [10, 20, 30, 40, 50]\r\nThe given list after reversing : [50, 40, 30, 20, 10]<\/pre>\n
Reversing a list Using Slicing Operator<\/h3>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = ['good', 'morning', 'btechgeeks', 25, 35]\r\n# Print the given original list.\r\nprint(\"The Given original list is :\", gvn_lst)\r\n# Reverse the elements of the given list using the slicing operator and\r\n# store it in another variable.\r\nreversee_lst = gvn_lst[::-1]\r\n# Print the list after reversing all its elements.\r\nprint(\"The given list after reversing : \", reversee_lst)\r\n<\/pre>\n
The Given original list is : ['good', 'morning', 'btechgeeks', 25, 35]\r\nThe given list after reversing : [35, 25, 'btechgeeks', 'morning', 'good']<\/pre>\n
\n