Sort the List Of Strings Alphabetically
Below, are the various ways to alphabetically sort a list of strings in Python.
- Naive Approach
- Using Sort() Function
- Using sorted() function
Naive Approach
In this example, code defines a function naive_sort
to sort a list of strings alphabetically using a nested loop and swapping elements if they are out of order. It then creates a list of strings str_list
, applies the sorting function.
Python3
def naive_sort(strings_list): n = len (strings_list) # Iterate over each element in the list for i in range (n): # Compare each element with the remaining elements for j in range (i + 1 , n): # If the current element is greater than the next one, swap them if strings_list[i] > strings_list[j]: strings_list[i], strings_list[j] = strings_list[j], strings_list[i] str_list = [ "hello" , "world" , "learn" , "w3wiki" , "courses" , "python" ] naive_sort(str_list) print ( "Sorted List:" , str_list) |
Output :
Sorted List: ['courses', 'w3wiki', 'hello', 'learn', 'python', 'world']
Using sort() function
The sort() function sorts the elements of a list in ascending order by default. The sort() function sorts the list in place, meaning it will modify the original list.
Syntax : <list_object>.sort(reverse)
Sort in Alphabetically Order : Below, given code initializes a list of strings (str_list
), sorts it alphabetically using the sort()
method, and then prints the sorted list.
Python3
str_list = [ "hello" , "world" , "learn" , "w3wiki" , "courses" , "python" ] print ( "Original list:" , str_list) # Sort the list str_list.sort() print ( "Sorted list:" , str_list) |
Output :
Original list: ['hello', 'world', 'learn', 'w3wiki', 'courses', 'python']
Sorted list: ['courses', 'w3wiki', 'hello', 'learn', 'python', 'world']
Using sorted() function
The sorted() function sorts the elements of a list passed to it in ascending order by default. The sorted() function sorts returns a new sorted list, which means it will leave the original list unchanged.
Syntax : sorted(<list_object>, reverse)
Sort in Alphabetically Order : Below code first prints the original list and then prints the sorted version of the list using the sorted()
function, demonstrating the strings arranged in ascending alphabetical order.
Python3
str_list = [ "JavaScript" , "Java" , "ReactJS" , "Flask" , "Flutter" ] print ( "Original List:" , str_list) sorted_str_list = sorted (str_list) print ( "Sorted List:" , sorted_str_list) |
Output :
Original List: ['JavaScript', 'Java', 'ReactJS', 'Flask', 'Flutter']
Sorted List: ['Flask', 'Flutter', 'Java', 'JavaScript', 'ReactJS']
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, the strings that start with uppercase characters(A-Z) come before strings that begin with lowercase characters(a-z). This means the string “Apple” will come before the string “apple” in alphabetical order, as “Apple” starts with an uppercase character.