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.

Was this post helpful?

Related Posts

  • 27 November

    Remove Punctuation from List in Python

    Table of ContentsUse for loop with replace() MethodUse Nested List ComprehensionUse str.translate() Function Use for loop with replace() Method To remove punctuation from list in Python: Use the for loop to iterate over each word (element) of given list. Use a nested loop to iterate over each character in the word. Use the if statement […]

  • 04 October

    Get Every Other Element in List in Python

    Table of ContentsGet Every Other Element in List in PythonUsing List SlicingUsing the list.append() function along with the range() functionUsing list comprehension and the range() function with conditionalsUsing list comprehension and the range() function with stepUsing the enumerate() function along with list comprehensionUsing a lambda functionConclusion. A list is one of the more versatile data […]

  • 23 September

    Add Tuple to List in Python

    Table of ContentsAdd tuple to list in PythonWays to Add Tuple to List in Python (adding the Tuple as An Element)Using the insert() Function to Add Tuple to List in PythonUsing the append() Function to Add Tuple to List in PythonWays to Add Tuple to List in Python (adding the Tuple’s Elements to A List)Using […]

  • 24 May

    Replace Comma with Space in List in Python

    Table of ContentsHow to replace comma with space in list in PythonUsing the replace() function to replace comma with space in list in PythonUsing the re.sub() function to replace comma with space in list in PythonUsing the split() and join() functions to replace comma with space in list in Python.Conclusion In this post, we will […]

  • 19 May

    Compare list elements with each other in Python

    Table of ContentsCompare all elements in list PythonWays to compare list elements with each other in PythonUsing the for loop to compare all elements in list PythonUsing the itertools.combinations() function to compare all elements in list PythonConclusion In this post, we will see how to compare list elements with each other in Python. Compare all […]

  • 18 May

    How to Deep Copy a List in Python

    Table of ContentsWhat is a list in Python?What are shallow copy and deep copy operations in Python?Shallow CopyDeep CopyHow to deep copy a list in Python?Using the deepcopy() function from the copy module to deep copy a list in Python.Using a user-defined function to deep copy a list in Python.Using list comprehension to deep copy […]

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.