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.
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']
Use `sort()` or `sorted()` Based on Your Needs.
- Use
sorted()
when:- Dealing with critical data that should not be altered.
- Needing the original data for future reference.
- Avoiding modifications to the original list.
- Willing to trade off additional memory space for a new sorted list.
- Use
sort()
when:- No future use of the original list is required.
- Prioritizing in-place sorting to save memory.
- Open to modifying the original list directly.
Both methods have their uses, and you can even use sorted()
when the original list won’t be used again. It ultimately depends on your specific needs and considerations.
Related Article:
Conclusion
In this article, we looked at two different ways of sorting a list of strings alphabetically in Python using the sort() and sorted() function with the help of examples. We also looked at using reverse=True as a parameter to sort the list of strings in reverse order. We also looked at which one is better to use when as both sort() and sorted() functions can help sort the list of strings alphabetically.