Reversing a List in Python

In Python, you can reverse the order of elements in a list by using the reverse method.

listname.reverse()

This method swaps the position of the first element with the last, the second element with the second-to-last, and so forth.

    A Practical Example

    Consider the following list:

    year = [2010, 2011, 2012, 2013, 2014]

    To reverse the list, you would type:

    year.reverse()

    After reversing, the list would look like this:

    [2014, 2013, 2012, 2011, 2010]

    The reverse method doesn't change the data within the list; it merely reverses the order of the elements.

     

     
     

    Please feel free to point out any errors or typos, or share suggestions to improve these notes. English isn't my first language, so if you notice any mistakes, let me know, and I'll be sure to fix them.

    FacebookTwitterLinkedinLinkedin
    knowledge base

    Python Lists

    1. What is a list
    2. Extracting elements from a list
    3. Removing an element
    4. Adding elements
    5. How to count occurrences in a list
    6. How to search for an element in a list
    7. Sorting a list
    8. Reversing the order of a list
    9. List comprehension
    10. Nesting list
    11. How to convert a tuple into a list