C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Python List reverse() MethodPython reverse() method reverses elements of the list. If the list is empty, it simply returns an empty list. After reversing the last index value of the list will be present at 0 index. The examples and method signature is given below. Signaturereverse() ParametersNo parameter ReturnIt returns None. Let's see some examples of reverse() method to understand it's functionality. Python List reverse() Method Example 1Let's first see a simple example to reverse the list. It prints all the elements in reverse order. # Python list reverse() Method # Creating a list apple = ['a','p','p','l','e'] # Method calling apple.reverse() # Reverse elements of the list # Displaying result print(apple) Output: ['e', 'l', 'p', 'p', 'a'] Python List reverse() Method Example 2It returns empty list if the list is the list is empty. See the example below. # Python list reverse() Method # Creating a list apple = [] # Method calling apple.reverse() # Reverse elements of the list # Displaying result print(apple) Output: [] Python List reverse() Method Example 3This example demonstrate that after reversing order of element does not change. # Python list reverse() Method # Creating a list apple = ['e', 'l', 'p', 'p', 'a'] apple2 = ['a', 'p', 'p', 'l', 'e'] # Calling Method apple.reverse() # Comparing both lists if apple == apple2: print("Both are equal") else: print("Not equal") Output: Both are equal
Next TopicPython Lists
|