How to Sort a List Alphabetically in Python?
Last Updated :
14 Apr, 2025
Sorting lists alphabetically is a common task in Python, whether we're organizing names, cleaning data or building an interface. Python makes sorting easy with several built-in methods and libraries for both simple and advanced sorting needs.
Let's look at some of the most common methods one by one:
Using sort() Method
The simplest way to sort a list alphabetically is by using sort() method. This method modifies the original list in place, arranging its elements in ascending order. sort() method is the most efficient in terms of both time and space since it sorts in place with minimal memory usage.
Python
a = ["Prajjwal", "Kareena", "Brijkant", "Aryan"]
a.sort()
print(a)
Output['Aryan', 'Brijkant', 'Kareena', 'Prajjwal']
Explanation: sort() method arranges the list elements in alphabetical order.
Note: Use sort() when you don’t need to retain the original list and want an efficient, in-place sorting solution.
Using sorted() Function
If you need to keep the original list unchanged and create a new sorted list, sorted() function is a better choice. It returns a new list with the sorted elements.
Python
a = ["Prajjwal", "Kareena", "Brijkant", "Aryan"]
b = sorted(a)
print(b)
Output['Aryan', 'Brijkant', 'Kareena', 'Prajjwal']
Using numpy.sort() for Large Datasets
The numpy library provides a highly optimized sorting function, particularly useful for large datasets.
Python
import numpy as np
a = ["Prajjwal", "Kareena", "Brijkant", "Aryan"]
b = np.sort(a)
print(b)
Output['Aryan' 'Brijkant' 'Kareena' 'Prajjwal']
Sorting a List of Strings with Different Cases and Lengths
By default, Python's sorting is case-sensitive. To sort alphabetically while ignoring case, use key=str.lower parameter. You can sort a list based on the length of its elements using len function as the key.
Python
a = ["Prajjwal", "Kareena", "Brijkant", "Aryan"]
b = sorted(a, key=str.lower)
print(b)
b.sort(key=len)
print(b)
Output['Aryan', 'Brijkant', 'Kareena', 'Prajjwal']
['Aryan', 'Kareena', 'Brijkant', 'Prajjwal']
Explanation:
- sorted(a, key=str.lower) sorts the list lexicographically.
- sort(key=len) sorts the list based on the length of each element in the list.
For more complex sorting tasks, you can split a list into chunks and sort each chunk individually.
Python
from itertools import islice
a = ["Prajjwal", "Kareena", "Brijkant", "Aryan"]
# Split and sort in chunks of 2
it = iter(a)
b = [sorted(list(islice(it, 2))) for _ in range(0, len(a), 2)]
print(b)
Output[['Kareena', 'Prajjwal'], ['Aryan', 'Brijkant']]
Explanation:
- it = iter(a) creates an iterator over the list.
- islice(it, 2) takes 2 elements at a time from the iterator.
- sorted(list(...)) sorts each chunk alphabetically (i.e., in lexicographical order).
Also read: Python, sort(), sorted(), np.sort(), iter(), islice().
Similar Reads
How To Sort List Of Strings In Alphabetically When working with Python programs, be it any machine learning program or simple text processing program, you might have come across a need to sort a list of strings alphabetically, either in ascending or reverse order. Strings are sorted alphabetically based on their initial letter (a-z or A-Z). But
3 min read
Python program to sort a list of tuples alphabetically Given a list of tuples, write a Python program to sort the tuples alphabetically by the first item of each tuple. Examples: Input: [("Amana", 28), ("Zenat", 30), ("Abhishek", 29), ("Nikhil", 21), ("B", "C")] Output: [('Amana', 28), ('Abhishek', 29), ('B', 'C'), ('Nikhil', 21), ('Zenat', 30)] Input:
3 min read
How to sort a list of strings in Python In this article, we will explore various methods to sort a list of strings in Python. The simplest approach is by using sort().Using sort() MethodThe sort() method sorts a list in place and modifying the original list directly.Pythona = ["banana", "apple", "cherry"] # Sorting list in place a.sort()
2 min read
Python Program to Sort Words in Alphabetical Order 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() m
2 min read
Python | Sort list containing alphanumeric values Given a list containing both alphanumeric values, write a Python program to sort the given list in such a way that the alphabetical values always comes after numeric values. Examples: Input : ['k', 5, 'e', 3, 'g', 7, 0, 't'] Output : [0, 3, 5, 7, 'e', 'g', 'k', 't'] Input : [1, 'c', 3, 2, 'a', 'b']
4 min read
Python | Ways to sort letters of string alphabetically Given a string of letters, write a python program to sort the given string in an alphabetical order. Example: Input : PYTHON Output : HNOPTY Input : Geeks Output : eeGksNaive Method to sort letters of string alphabetically Here we are converting the string into list and then finally sorting the enti
2 min read