Python Program to Sort Words in Alphabetical Order (original) (raw)

Last Updated : 23 Jul, 2025

The task is to write a program that takes a list of words as input and sorts them in alphabetical order. The program should display the sorted list of words, ensuring that the sorting is case-insensitive. To sort words in alphabetical order in Python, we can use the sorted() function or the sort() method, both of which can sort a list of strings in ascending order.

**Using sorted() Function

sorted() function returns a new list containing the sorted words, leaving the original list unchanged.

Python `

li = ["banana", "apple", "orange", "grape", "kiwi"] s = sorted(s) # all sorted words print(s)

`

Output

['apple', 'banana', 'grape', 'kiwi', 'orange']

**Explanation

Using sort() Method

sort() method sorts the list in place, modifying the original list.

Python `

li = ["banana", "apple", "orange", "grape", "kiwi"] li.sort() print(li)

`

Output

['apple', 'banana', 'grape', 'kiwi', 'orange']

**Explanation

**Using lambda Function

We can define custom sorting behavior using a lambda function. For example, we can sort words by their lengths and then alphabetically.

Python `

li = ["banana", "apple", "orange", "grape", "kiwi"] s = sorted(li, key=lambda x: (len(x), x)) print(s)

`

Output

['kiwi', 'apple', 'grape', 'banana', 'orange']

**Explanation