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.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

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

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more