To sort a list of strings in Python in dictionary order, you can use the `sorted` function.
In this example,
words1
with some strings.sorted
function to sort the list in dictionary order.words1 = ["banana", "apple", "grape", "orange"]
sorted_words1 = sorted(words1)
print("Sorted list 1 in dictionary order:", sorted_words1)
Sorted list 1 in dictionary order: ['apple', 'banana', 'grape', 'orange']
In this example,
words2
with different strings.sorted
function to sort this list in dictionary order.words2 = ["pear", "kiwi", "lemon", "apple"]
sorted_words2 = sorted(words2)
print("Sorted list 2 in dictionary order:", sorted_words2)
Sorted list 2 in dictionary order: ['apple', 'kiwi', 'lemon', 'pear']
In this tutorial, we learned How to Sort a List of Strings in Dictionary Order in Python language with well detailed examples.