Table of contents
1.
Introduction
2.
Lexicographic Order in Python
2.1.
Python
2.2.
Python
3.
Sorting Words in Lexicographic Order:
3.1.
Python
4.
Sorting a String with Lexicographic Order
4.1.
Python
4.2.
Python
5.
Sorting Python Lists in Lexicographic Order:
5.1.
Python
5.2.
Python
5.3.
Python
6.
Frequently Asked Questions
6.1.
What happens when two words have the same characters but different lengths in lexicographic order?
6.2.
Can we sort a list of numbers in lexicographic order?
6.3.
Can we sort a list in reverse lexicographic order?
7.
Conclusion
Last Updated: Jul 10, 2024
Medium

Lexicographical Order in Python

Author Riya Singh
0 upvote

Introduction

In Python, lexicographic order is a way to sort words or strings in a specific order. It is also known as alphabetical order or dictionary order. When we sort words in lexicographic order, we compare the characters of the words from left to right. This means that we first compare the first character of each word, then the second character, and so on until we find a difference or reach the end of a word. Lexicographic order is used to sort words in a dictionary, names in a phonebook, or any list of strings. 

Lexicographical Order in Python

In this article, we will learn how to sort words, strings, and lists in lexicographic order using Python.

Lexicographic Order in Python

In Python, we can use the built-in sorted() function or the sort() method of lists to sort elements in lexicographic order. By default, these functions sort the elements in ascending order. 

For example:

  • Python

Python

words = ["apple", "banana", "cherry", "date"]

sorted_words = sorted(words)

print(sorted_words)
You can also try this code with Online Python Compiler
Run Code


Output:

['apple', 'banana', 'cherry', 'date']


In this example, we have a list of words called words. We pass this list to the sorted() function, which returns a new sorted list. The words are sorted in lexicographic order, with "apple" coming first, followed by "banana", "cherry", and "date".

We can also use the sort() method to sort the list in place:

  • Python

Python

words = ["apple", "banana", "cherry", "date"]

words.sort()

print(words)
You can also try this code with Online Python Compiler
Run Code


Output:

['apple', 'banana', 'cherry', 'date']


The sort() method modifies the original list and sorts its elements in lexicographic order.

Sorting Words in Lexicographic Order:

To sort words in lexicographic order, we can simply use the sorted() function or the sort() method as shown in the previous examples. However, let's take a closer look at how the lexicographic order works.

When comparing two words, the lexicographic order starts by comparing the first character of each word. If the first characters are different, the word with the smaller ASCII value of the first character comes first. If the first characters are the same, the second characters are compared, and so on.

For example, let's compare the words "apple" and "banana":

Compare the first characters: "a" comes before "b", so "apple" comes first.

Now, let's compare the words "cherry" and "date":

Compare the first characters: "c" comes before "d", so "cherry" comes first.

If one word is a prefix of the other word, the shorter word comes first. For example, "app" would come before "apple".

Here's an example that demonstrates sorting words in lexicographic order:

  • Python

Python

words = ["banana", "apple", "date", "cherry"]

sorted_words = sorted(words)

print(sorted_words)
You can also try this code with Online Python Compiler
Run Code


Output

['apple', 'banana', 'cherry', 'date']


In this example, the words are sorted in lexicographic order. "apple" comes first because it has the smallest lexicographic order, followed by "banana", "cherry", and "date".

Sorting a String with Lexicographic Order

In Python, we can also sort the characters of a string in lexicographic order. To do this, we can convert the string to a list of characters, sort the list, and then join the characters back into a string.

For example:

  • Python

Python

string = "delhi"

sorted_string = ''.join(sorted(string))

print(sorted_string)
You can also try this code with Online Python Compiler
Run Code


Output:

Delhi


In this example, we have a string called string with the value "openai". We use the sorted() function to sort the characters of the string in lexicographic order. The sorted() function returns a list of sorted characters. We then use the join() method to join the characters back into a string.

The resulting sorted_string contains the characters of the original string sorted in lexicographic order.

We can also sort a string directly using the sorted() function and passing the string as an argument:

  • Python

Python

string = "mumbai"

sorted_string = ''.join(sorted(string))

print(sorted_string)
You can also try this code with Online Python Compiler
Run Code


Output:

abimmu


This gives the same result as the previous example.

Sorting Python Lists in Lexicographic Order:

In Python, we can sort lists that contain strings or other comparable elements in lexicographic order using the sorted() function or the sort() method.

Here's an example of sorting a list of strings in lexicographic order:

  • Python

Python

fruits = ["banana", "apple", "orange", "grape"]

sorted_fruits = sorted(fruits)

print(sorted_fruits)
You can also try this code with Online Python Compiler
Run Code


Output:

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


In this example, we have a list called fruits that contains strings representing different fruits. We use the sorted() function to sort the list in lexicographic order. The sorted() function returns a new sorted list, which we store in the sorted_fruits variable.

We can also use the sort() method to sort the list in place:

  • Python

Python

fruits = ["banana", "apple", "orange", "grape"]

fruits.sort()

print(fruits)
You can also try this code with Online Python Compiler
Run Code


Output:

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


The sort() method modifies the original fruits list and sorts its elements in lexicographic order.

We can also sort lists that contain tuples or other objects based on specific attributes.

For example, let's say we have a list of tuples representing names and ages:

  • Python

Python

people = [("Rahul", 25), ("Rinki", 30), ("Harsh", 20)]

sorted_people = sorted(people)

print(sorted_people)
You can also try this code with Online Python Compiler
Run Code


Output:

[('Harsh', 20), ('Rahul', 25), ('Rinki', 30)]


In this example, the list people contains tuples where each tuple represents a person's name and age. When we sort the list using sorted(), the tuples are sorted based on the first element of each tuple (the name) in lexicographic order. If the names are the same, the second element (the age) is used for sorting.

Frequently Asked Questions

What happens when two words have the same characters but different lengths in lexicographic order?

In lexicographic order, if two words have the same characters up to the length of the shorter word, the shorter word comes first. For example, "apple" would come before "apples" in lexicographic order because "apple" is shorter.

Can we sort a list of numbers in lexicographic order?

Yes, we can sort a list of numbers in lexicographic order. However, when sorting numbers, the lexicographic order treats them as strings. For example, [10, 2, 5] would be sorted as [10, 2, 5] in lexicographic order because "1" comes before "2" and "5". If you want to sort numbers in numerical order, you should use the default sorting order or specify a key function.

Can we sort a list in reverse lexicographic order?

Yes, we can sort a list in reverse lexicographic order by passing the reverse=True argument to the sorted() function or the sort() method.

Conclusion

In this article, we have learned about lexicographic order in Python. We discussed how to sort words, strings, and lists in lexicographic order using the sorted() function and the sort() method. We also looked at examples of sorting words, sorting the characters of a string, and sorting lists of strings or tuples. 

You can also practice coding questions commonly asked in interviews on Coding Ninjas Code360

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass