Built with Tutify.io

    AIStudyLabs

    How to Sort List of Strings in Python

    How to Sort List of Strings in Python

    To sort a list of strings in Python you can use the sort() method. This method will order the list of strings in place, meaning that it will modify the original list and you won’t need to create a new list. You can also use the sorted() function to sort a list of strings, this returns a new list after sorting.

    There are a few ways to sort the list of strings in Python.

    1. Quick Examples of Sort List of Strings

    If you are in a hurry, below are some quick examples of how to sort list of strings in Python.

    
    # Quick examples of sort list of strings
    
    # Example 1: Sort list of strings
    technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy','Hyperion']
    technology.sort() 
    
    # Example 2: Sort list by length of strings 
    technology.sort(key = len)
    
    # Example 3: Sort string by integer value use key as int
    strings = ['12','34','5','26','76','18','63']
    strings.sort(key = int)
    
    # Example 4: Sort string in reverse order 
    technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy','Hyperion']
    technology.sort(reverse = True)
    
    # Example 5: Using sorted() method
    sorted_list = sorted(technology)
    
    # Example 6: Sorted string by integer value use key = int
    strings = sorted(strings, key=int)
    
    # Example 7: Sorted list of strings in descending order
    technology = sorted(technology, reverse=True)
    

    <a id="h-2-python-sort-list-of-strings"></a>

    2. Python Sort List of Strings

    The sort() function is used to sort the list of strings in ascending order in Python. This function modifies the original list in place and does not return a new list. For instance, using the sort() function without any parameters to sort a list of strings in ascending order.

    
    # Use sort() method
    technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy','Hyperion']
    technology.sort() 
    print("After sorting the list of strings:\n",technology)
    

    Yields below output.

    <a id="h-3-sort-list-of-strings-by-sorted-method"></a>

    3. Sort List of Strings by sorted() Method

    Alternatively, you can use the python sorted() built-in function to order the technology list of stings. It returns a new list containing all the elements from the technology list in sorted order.

    In the below example, the sorted() function is used to create a new sorted list (sorted_list) from the original list (technology). The original list remains unchanged.

    
    # Using sorted() method
    technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy','Hyperion']
    sorted_list = sorted(technology)
    print("After sorting the list of strings:\n",sorted_list)
    

    Yields the same output as above.

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

    4. Sort List by Length of Strings

    To sort a list of strings based on the length of the strings, you can use the len() function as the key for the sort() function. For example, the list is sorted based on the length of the strings, with the shortest string coming first and the longest string coming last.

    
    # Sort list by length of strings 
    technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy','Hyperion']
    technology.sort(key = len)
    print("After sorting the list of strings by length:\n", technology)
    
    # Output:
    # After sorting the list of strings by length:
    #  ['Java', 'Spark', 'NumPy', 'Hadoop', 'Pandas', 'Pyspark', 'Hyperion']
    

    <a id="h-5-sort-strings-using-a-function"></a>

    5. Sort Strings Using a Function

    Similarly, If you want to sort a list of strings based on the integer value of the strings, you can convert the strings to integers by using int() function as the key for the sort() method or sorted() function. This sorts the list by numbers.

    
    # Sort string by integer value use key as int
    strings = ['12','34','5','26','76','18','63']
    strings.sort(key = int)
    print("After sorting the list of strings by integer value:\n",strings)
    
    # Sorted string by integer value use key=int
    strings = sorted(strings, key=int)
    print("After sorting the list of strings by integer value:\n",strings)
    
    # Output:
    # After sorting the list of strings by integer value:
    #  ['5', '12', '18', '26', '34', '63', '76']
    

    <a id="h-6-sort-strings-in-descending-order"></a>

    6. Sort Strings in Descending Order

    To sort a list of strings in descending or reverse order, you can pass the reverse=True argument to the sort() method or sorted() function. Descending order is the opposite of ascending order where elements are arranged from highest to lowest value (for string Z to A).

    
    # Sort in descending order 
    technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy','Hyperion']
    technology.sort(reverse = True)
    print("Sorted list in descending order:\n", technology)
    
    # Sorted list of strings in descending order
    technology = ['Java','Hadoop','Spark','Pandas','Pyspark','NumPy','Hyperion']
    technology = sorted(technology, reverse=True)
    print("Sorted list in descending order:\n", technology)
    
    # Output:
    # Sorted list in descending order:
    #  ['Spark', 'Pyspark', 'Pandas', 'NumPy', 'Java', 'Hyperion', 'Hadoop'
    

    <a id="h-frequently-asked-questions-on-sort-list-of-strings-in-python"></a>

    Frequently Asked Questions on Sort List of Strings in Python

    You can use either the sorted() function or the sort() method. The sorted() function creates a new sorted list, while the sort() method sorts the list in-place.

    You can sort a list of strings in descending order in Python. Both the sorted() function and the sort() method have a reverse parameter that you can set to True to achieve descending order.

    You can use the key parameter with a custom function to sort the list based on a specific criterion. To sort a list of strings based on a custom criterion, you can use the key parameter of the sorted() function or the sort() method. The key parameter allows you to specify a function that calculates a value for each element, and the sorting is then based on those values.

    To sort strings in descending order based on a custom criterion, you can use the key parameter in conjunction with the sorted() function or the sort() method. The key parameter allows you to specify a function that calculates a value for each element, and the sorting is then based on those values.

    You can sort a list of strings in-place without creating a new list using the sort() method. The sort() method modifies the original list directly. For example, the sort() method is called on the my_list object, and it sorts the elements of the list in ascending order. If you want to sort the list in descending order, you can use the reverse=True parameter

    <a id="h-conclusion"></a>

    Conclusion

    In this article, I have explained how to sort the list of strings in Python, first, I have covered using list.sort() function and Python built-in function sorted().

    Happy Learning !!

    Related Articles

    References