Python Examples

Python Program to Sort Words in Alphabetical Order

With the help of python sort list alphabetically code, one can arrange a given list of words in alphabetical order, as arranged in a dictionary. The same algorithm with some changes can also be used to reverse the order and arrange the words from Z to A. 

python sort list alphabetically


Python Sort List Alphabetically Program

To understand the code of sort alphabetically Python, one must understand the use of loops and methods that are in-built in the Python library for strings. The program may use methods such as len (), which finds the length of the string, or the lower () method. 

Source Code 

For the source code given below, we first input a string. The string, that is a sentence, is then broken down into a list of words. Using the sort () function, we can arrange all the words present in the string in alphabetical order. 


str_1 = input (“Enter a string” “)

words = [word.lower () for word in str_1.split ()]

words.sort ()

print (“The words sorted in alphabetical order are as follows: “)

 for word in words:

              print (word)

In the program, the string entered by the user is stored in the variable named str_1. You can input different strings to test the program. The sort () method is in-built in the Python library and is used to arrange a list of words or characters in alphabetical order. 

If one wants to sort the letters that are present in a word, then they can use the sort method () such as shown in the program below. 


def sortString (str)

              return ‘ ‘.join (sorted (str))

str = input (“Enter any word that you want to sort in alphabetical order”)

print (sortString (str))

Here, we have used the sorted () method instead of the sort () method. You can use the sorted () method with accumulate () and reduce () as well. The program will provide the same results. Here are some sample codes.

Using accumulate () method:


from itertools import accumulate 

def sortString (str):

              return tuple (accumulate (sorted (str)))[-1]

str = input (“Enter a string that you want to arrange alphabetically: “)

print (sortString (str))

Using reduce () method:


from functools import reduce 

def sortString (str):

              return reduce (lambda a, b : a + b, sorted (str))

str = input (“Enter a string that you want to arrange in alphabetical order: “)

print (sortString (str))

Through the reduce () method, the join function is used on the sorted list with the ‘+’ operator. 

Thus, there are different methods available in the Python library which can be used to obtain a sorted list in alphabetical order.  

FAQs on python sort list alphabetically

How do I sort a list alphabetically?

To arrange a list in alphabetical order, assess the first letter of the words present in the list and then arrange them as per the alphabets from A to Z. If two or more words have the same first letter, then compare their second letter and arrange them accordingly. Repeat the process until all the words are arranged in the required order. 

How do you sort a list alphabetically?

Using the sort () and sorted () method, one can sort alphabetically Python a list of words. 

Share with friends

Customize your course in 30 seconds

Which class are you in?
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
Ashhar Firdausi
IIT Roorkee
Dr. Nazma Shaik
Gaurav Tiwari
Get Started

Leave a Reply

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

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.