XX12
XX12

Reputation: 11

Lexicographic order in python

'tactic' <= 'tree'

Can i know why 'tactic' is lexicographically smaller than 'tree' / why will it return true for the code 'tactic'<='tree' ?

From my understanding , isn't it supposed to return false since the third index of both strings , where it's supposed to be ( t is > than e ) ? Hence returning a false ?

Upvotes: 0

Views: 261

Answers (1)

Philipp P
Philipp P

Reputation: 109

The reason for this behavior is that the lexicographic order is computed from front to back. The first charater that is different defines the order of the items.

In your example it is evaluated this way:

  • first character from each string is 't' => equal, next character
  • second character is 'a' and 'r' => 'a' < 'r', this means the first string is smaller => 'tactic' < 'tree'

Upvotes: 1

Related Questions