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

  • Initialize list with zeros in Python
    25 October

    Four Ways To Initialize List With Zeros In Python

    Table of ContentsWhy Initialize List with Zeros in Python?How Can We Initialize List With Zeros In Python?Initialize List With Zeros Using * Operator In PythonInitialize List With Zeros Using  itertools.repeat() FunctionInitialize List With Zeros Using  Generator Comprehension In PythonInitialize List With Zeros Using  List Comprehension In PythonConclusion Lists in Python are one of the most […]

  • 06 October

    How to find index of all occurrences in list in Python

    Table of ContentsThe easiest method – Using list count() methodThe Basic Method – Using for loopsThe shortest method – Using the enumerate() functionThe fastest method – Using a numpy arrayUsing itertools moduleUsing more-itertools LibraryUsing index() function List is a data type in Python. It is used to store multiple values in one variable. If you […]

  • Convert list to matrix in Python
    21 August

    Convert list to matrix in Python

    Table of ContentsUsing the while loop to convert list to matrix in Python.Use the numpy.array() method to convert list to matrix in Python.Using the numpy.asarray() method to convert list to matrix in Python. Python provides some in-built data types to stock multiple items in a single variable. A List is one among these built-in data […]

  • Find common elements in two lists Python
    25 May

    Find common elements in two lists in python

    Table of ContentsUsing the intersection() FunctionUsing List ComprehensionUsing & Operator 💡 Outline You can first convert first list into set using set() and call intersection() by passing second list as parameter to find common element in two lists in Python. [crayon-61774f3f5e91b464482847/] In this tutorial, we will see different methods to find common elements in two […]

  • 11 April

    Python Remove Newline from List

    Table of ContentsIntroduction✨ Method 1: Using the replace() method✨ Method 2: Using re.sub()✨ Method 3: Using strip()✨ Method 4: Slice the List Elements✨ Method 5: Using List Comprehension and Splitlines Function✨ Method 6: Using str.maketrans(x,y) and str.translate(map)Conclusion 🎯 Objective: In this article, we will discuss the methods to remove ‘\n’ or newline characters from a […]

  • 06 April

    Print Matrix in Python

    In this article you will learn "How to print a matrix in Python?"

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.