Sort list alphabetically in Python

Sort list alphabetically in Python

💡 Outline
You can use sort() method of list to sort list alphabetically in Python

In this tutorial, we will discuss different ways to sort a list namely using the sort() function, sorted() function, and implementing the quick sort algorithm.

Use of the sort() function to sort list alphabetically in Python.

The sort() method can be utilized to sort the elements in a given list in ascending or descending order. This method performs the sorting on the object itself and makes changes to the values of the original list.

The sort() method’s syntax includes two optional parameters, namely key, and reverse. The key parameter, as its name suggests, sets out as a key for the sorting process. The reverse parameter is utilized to dictate the order in which the sorting is to be done. In the case of reverse value being true, the sorting occurs in reverse or descending order.

However, the sort() function executes perfectly even without using the aforementioned optional parameters.

The following code uses the sort() method to sort the elements of a list in Python.

Output:

[‘Envy’, ‘Gluttony’, ‘Sloth’, ‘pride’, ‘wrath’]

To implement this in the reverse or descending order, a small tweak needs to be done on the above code.

The following code uses the sort() method to sort the elements of a list in Python in decreasing order.

Output:

[‘wrath’, ‘pride’, ‘Sloth’, ‘Gluttony’, ‘Envy’]

When the sorting process occurs, the words starting with an uppercase letter have a higher precedence than words starting with a lowercase letter, and sorting of the list occurs according to this precedence.

The optional key parameter can also be used in the Python code depending on the needs of the programmer. In the code below, the sorting process is done taking the length of the string as the key in the code.

Output:

[‘Envy’, ‘wrath’, ‘Sloth’, ‘pride’, ‘Gluttony’]

Use of the sorted() function sort list alphabetically in Python.

Python also provides an in-built sorted() function that serves as an alternative and is able to achieve the same result as the sort() function.

While the sort() function does not return any particular value and makes the operational changes to the original list, the sorted() function generates a new sorted list rather than making any adjustments or alterations to the original list.

The following code uses the sorted() function to sort the elements of a list in Python.

Output:

[‘Envy’, ‘Gluttony’, ‘Sloth’, ‘pride’, ‘wrath’]

Sorting in descending order is also possible using this method. Just like the sort() method, this method also takes the reverse parameter.

Its implementation is shown in the following code:

Output:

[‘wrath’, ‘pride’, ‘Sloth’, ‘Gluttony’, ‘Envy’]

Just like in the sort() function, the key parameter is also accepted in the sorted() function and is implemented in the following code:

Output:

[‘Envy’, ‘wrath’, ‘Sloth’, ‘pride’, ‘Gluttony’]

Use of the QuickSort algorithm sort list alphabetically in Python.

We can simply use a sorting algorithm for the purpose of sorting the elements of a list in Python. Sorting techniques like bubble sort, merge sort, heap sort, and many others can also be used to perform this operation instead of the quick sort algorithm. Quick Sort is the fastest algorithm out of all the others and is, therefore, the chosen method in this explanation.

The following code implements the QuickSort algorithm to sort the elements of a list in Python.

Output:

[‘Envy’, ‘Gluttony’, ‘Sloth’, ‘pride’, ‘wrath’]

That’s all about how to sort list alphabetically in Python.

Related Posts

  • Create a list from 1 to 100 in Python
    03 May

    Create a List from 1 to 100 in Python

    Table of ContentsWays to create a list from 1 to 100 in PythonUsing the range() function to create a list from 1 to 100 in PythonUsing the numpy.arange() function to create a list from 1 to 100 in PythonUsing the for loop with range() to create a list from 1 to 100 in PythonConclusion In […]

  • 07 January

    How to compare lists in Python

    Table of ContentsPython compare listsHow to compare lists in PythonUsing the sort() function and == operatorUsing the map() and reduce() functionsUsing the set() function and == operatorUsing the for loopUsing the set() function and - operatorUsing the collections.Counter() class and == operatorUsing the zip(), sum(), and len() functionsUsing the cmp() functionConclusion In this post, we […]

  • Python List of dictionaries
    09 December

    List of Dictionaries in Python

    Table of ContentsHow to Create a List of Dictionaries in Python?How to Access Elements of a List of Dictionaries?Search in a List of Dictionaries in PythonUpdate Values in a List of Dictionaries in PythonHow to Sort List of Dictionaries in Python?Conclusion We use both lists and dictionaries in our python programs. Lists are used to […]

  • Python sort list of lists
    17 November

    Sort List of Lists in Python

    Table of ContentsSort List of Lists Using the sort() Method in PythonUsing the itemgetter() methodUsing Lambda FunctionsSort List of Lists Using the sorted() Function in PythonSort lists of lists on the basis of the length of the inner listsConclusion Several times we use 2-D lists or lists of lists in our programs to represent a […]

  • Remove nan from list in Python
    16 November

    Remove nan From List In Python?

    Table of ContentsWhat is nan in python?How to remove nan from list in python?Remove nan from list using the str() functionRemove nan from list using math.isnan() functionRemove nan from list using numPy.isnan() functionRemove nan from list using pandas.isnull() functionConclusion In python, nan is a special value used to represent missing values. In this article, we […]

  • List intersection in Python
    11 November

    List intersection in python

    Table of ContentsWhat is the list intersection in Python?Ways to find the list intersection in PythonUsing the for loop to achieve list intersection in PythonUsing the set() function to find list intersection in PythonUsing the intersection() function to find list intersection in PythonUsing the filter() and lambda functions to find list intersection in PythonUsing the […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.