Lexicographical order in Python

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.
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
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.
Scaler Placement Report and Statistics
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
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:
Turn Learning into Career Growth
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.