If you are working on a list of arbitrary lengths, I think sorted() , as the other answers indicate, is good for small lists (with small lines) when it comes to large lists and large lines and cases (and cases where the list can be randomly ordered), a faster way is to use the all() built-in function and the generator expression (this should be faster than sorted() ). Example -
#Assuming list is called lst print(all(lst[i].lower() < lst[i+1].lower() for i in range(len(lst)-1)))
Note that the above will eventually call str.lower() for each row (except the first and last) twice. If your lines are not very large, this should be fine.
If your lines are really very large compared to the length of the list, you can create another temporary list before doing all() , which stores all the lines in lower case. And then run the same logic on this list.
You can create your list (using user input) using list comprehension, Example -
lst = [input("Enter word {}:".format(i)) for i in range(3)]
Results of synchronization of the specified method vs sorted() (modified sorted() code for working without case) -
In [5]: lst = ['{:0>7}'.format(i) for i in range(1000000)] In [6]: lst == sorted(lst,key=str.lower) Out[6]: True In [7]: %timeit lst == sorted(lst,key=str.lower) 1 loops, best of 3: 204 ms per loop In [8]: %timeit all(lst[i].lower() < lst[i+1].lower() for i in range(len(lst)-1)) 1 loops, best of 3: 439 ms per loop In [11]: lst = ['{:0>7}'.format(random.randint(1,10000)) for i in range(1000000)] In [12]: %timeit lst == sorted(lst,key=str.lower) 1 loops, best of 3: 1.08 s per loop In [13]: %timeit all(lst[i].lower() < lst[i+1].lower() for i in range(len(lst)-1)) The slowest run took 6.20 times longer than the fastest. This could mean that an intermediate result is being cached 100000 loops, best of 3: 2.89 ยตs per loop
Result -
In cases that should return True (these are already sorted lists), using sorted() is much faster than all() , since sorted() works great for lists sorted for the most part.
For random cases, all() works better than sorted() due to a short circuit all() (it will short circuit when it sees the first False ).
In addition, there is the fact that sorted() will create a temporary (sorted list) in memory (for comparison), while all() will not require this (and this fact applies to the timings that we see above).
An earlier answer, which is direct (and applies only to this question), you can simply directly compare strings as such, you do not need another line / list for alphabets. Example -
first = (input('Enter first word: ')) second = (input('Enter second word: ')) third = (input('Enter third word: ')) if first <= second <= third: print(True)
Or, if you want to compare only the first characters (although I really doubt it) -
if first[0] <= second[0] <= third[0]: print(True)
To compare case insensitive strings, you can convert the entire string to lowercase before comparing. Example -
if first.lower() <= second.lower() <= third.lower(): print(True)
Or easier -
print(first.lower() <= second.lower() <= third.lower())