Built with Tutify.io

    AIStudyLabs

    Python List sort() Method

    Python List sort() Method

    The Python list.sort() method is a built-in function that is used to sort a list in ascending or descending order. By default, the sort() method sorts the elements of a list in ascending order. However, you can use the reverse parameter to sort the list in descending order.

    Related: Python sorted() Function

    In this article, I will explain the syntax of the Python list sort() method, its parameters and explain how to sort a list in ascending, descending, and user-defined order with examples.

    1. Quick Examples of List sort() Method

    If you are in a hurry, below are some quick examples of the python list sort() method.

    
    # Below are the quick examples
    
    # Example 1: Sort list by ascending order
    technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy']
    technology.sort()
    
    # Example 2: Sort the list in descending order
    technology.sort(reverse=True)
    
    # Example 3: Sort a list of strings in descending order
    technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy']
    technology.sort(key=len, reverse=True)
    
    # Example 4: Sort a list of Integers in ascending order
    numbers = [5, 2, 8, 3, 6, 9]
    numbers.sort()
    
    # Example 5: Sorting using user-defined order 
    def get_length(val):
      return val[1]
    
    # Sorts the array in ascending according to
    # get_length element
    myList = [(2, 3), (4, 6), (1, 5)]
    myList.sort(key=get_length)
    
    # Example 6: Sorts the array in ascending according to
    # get_length element
    sort_list = sorted(myList, key=get_length)
    

    <a id="h-2-syntax-of-sort-method"></a>

    2. Syntax of sort() Method

    Following is the syntax for creating sort() method

    
    # Syntax of sort()  function
    list.sort(reverse=True/False, key=myFunc)
    

    2.1 Parameter of sort()

    The sort() method takes the following optional parameters.

    <a id="h-3-sort-list-of-strings-in-ascending-order"></a>

    3. Sort List of Strings in Ascending Order

    By using the Python list sort() function let's sort the list of strings in ascending order, For strings ascending order means it sorts in alphabetical order. The sort() method by default sorts in ascending order

    
    # Sort the list in ascending order
    technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy']
    technology.sort()
    print(technology)
    
    # Output:
    # ['Hadoop', 'Java', 'NumPy', 'Pandas', 'Pyspark', 'Spark']
    

    4. Sort List of Strings in Descending Order

    To sort a list of strings in descending order, you can use the sort() method with the reverse argument set its value to reverse=True. Descending order is the opposite of ascending order where elements are arranged from highest to lowest value.

    
    # Sort the list of alphabets in descending order
    technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy']
    technology.sort(reverse=True)
    print(technology)
    
    # Output:
    # ['Spark', 'Pyspark', 'Pandas', 'NumPy', 'Java', 'Hadoop']
    

    <a id="h-5-sort-list-of-strings-by-length"></a>

    5. Sort List of Strings by Length

    You can pass the function to the key to sort a Python list in a custom order. For example, pass key=len to sort a list of strings by length. You can also use the reverse parameter to specify whether the sort should be in ascending or descending order. The following example sorts the list of items based on their length in descending order.

    
    # Sort a list of strings in descending order
    technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy']
    technology.sort(key=len, reverse=True)
    print(technology)
    
    # Output:
    # ['Pyspark', 'Hadoop', 'Pandas', 'Spark', 'NumPy', 'Java']
    

    <a id="h-6-sort-list-of-integers-in-ascending-descending-order"></a>

    6. Sort List of Integers in Ascending & Descending Order

    Let's also see an example of sorting integers in ascending order. It is a stable sort, meaning that it preserves the original order of equal elements.

    
    # Sort a list of Integers in ascending order
    numbers = [5, 2, 8, 3, 6, 9]
    numbers.sort()
    print(numbers)
    
    # Output
    # [2, 3, 5, 6, 8, 9]
    
    # Sort a list of Integers in descending order
    numbers.sort(reverse=True)
    print(numbers)
    
    # Output
    # [9, 8, 6, 5, 3, 2]
    

    <a id="h-7-python-list-sort-using-user-defined-order"></a>

    7. Python List Sort using User-defined Order

    Alternatively, you can use the Python list sort() method to order the list by custom order. Here, I will use the get_length() function. This function is used to extract the second element of each tuple in the list, and the list is sorted in ascending order based on these values.

    
    # Sorting using user-defined order 
    def get_length(val):
      return val[1]
    
    # Create list of tupple
    myList = [(2, 3), (4, 6), (1, 5)]
    
    # Sorts the array
    myList.sort(key=get_length)
    print(List)
    
    # Using sorted() function
    List = sorted(List, key=get_length)
    print(List)
    
    # Output:
    # [(2, 3), (1, 5), (4, 6)]
    

    You can call the sort() function on the list List and sorts it in descending order based on the values returned by the get_length() function. It takes a tuple val as input and returns the second element of the tuple. This element is used to determine the sort order of the list.

    
    # Sorts the array in descending according to
    # get_length element
    myList.sort(key=get_length, reverse=True)
    print(myList)
    
    # Output
    # [(4, 6), (1, 5), (2, 3)]
    

    Conclusion

    In this article, I have explained the Python list sort() method syntax, parameters, and how to sort the list of strings and integers in ascending and descending order, and also learned how to sort user-defined order with examples.

    Happy Learning !!

    Related Articles

    References