Lexicographical order in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

Lexicographical order in Python is the type of sorting in which the data elements appear in the dictionary order. In the lexicographical order, data elements are sorted based on alphabetical order.

Lexicographic Order in Python:

We can sort the data elements in Python using various sorting techniques. One such technique in python is to sort the data elements lexicographically. In the lexicographical order, data elements are sorted in the dictionary order, i.e., the first alphabet of data elements will be compared and sorted. If the alphabet is the same for a set of data elements, the comparison will be made based on the next alphabet for that set.

Sorting Words in Lexicographic Order:

The words can be sorted lexicographically in Python using the sorting function. Let’s say we have a list of two words, namely ["PANT", "DHONI"]. So if we want to sort them lexicographically, we will compare the first alphabet of each word present in the list. As D is alphabetically smaller than P, the lexicographically sorted list will have ["DHONI", "PANT"].

Let’s take the second example. If we have a list of two words as ["DRAVID", "DHONI"], in this case, as both have the same first alphabet, we will compare the second alphabet. As R is greater than H, our lexicographically sorted list of words will be ["DHONI", "DRAVID"].

In python, we have two functions to sort the data elements in lexicographical order. The two functions are sort() and sorted(). The sort() function sorts the data elements in-place whereas the sorted() funtion sort the data elements in separate array.

Example of lexicographical order using sort()

Code:

Output:

Example of Lexicographical Order Using sorted():

Code:

Output:

Note: In-place sorting means the operations are carried out on the input list or array itself.

Sorting a String with Lexicographic Order:

To sort the string in Python based on lexicographical order. We cannot directly apply the sort function. We need to split the string initially and apply the sort function.

Code:

Output:

Sorting Python Lists in Lexicographic Order

To sort the Python lists in lexicographical order, we need to initially sort the list based on lexicographical order and then again sort them based on length. This will ensure that list with the smallest size will be in the correct order.

Code:

Output:

Conclusion

  • To arrange the data elements in dictionary order is called lexicographical order.
  • Lexicographical order In Python is achieved by using sort() and sorted() function.
  • sort() function sorts data elements in lexicographical order by performing operations on the input list.
  • sorted() function sorts data elements in lexicographical order by replicating the input list and keeping the input list as it is.
  • Strings in Python can be sorted by initially splitting and applying sort.
  • List of lists in Python can be sorted by applying sorting for lexicographical order, followed by sorting based on length in order to ensure the smaller lists are displayed first.