How can I sort an item alphabetically before I add it to the list?

440 views Asked by At

I have an assignment that I am supposed to find each word in a line, and add that word to a list. Then there is also another list corresponding to the list of word, but that list will tell the amount of times the word appear in the text.

I have finished that part. However, I cannot find a way to compare the new found word to the word in the list, and find the index to insert it in the list in an alphabetical order. I know that I am supposed to write a function that will find that index in the list, so i can insert that item in both lists. I am not allowed to use the sort operator, so I am having a little trouble. Can anyone help me writing that one function using only conditions operators.

If I am not clear, please let me know.

1

There are 1 answers

0
Raymond Hettinger On

Homework hint: look at Python's source code for the bisect module. That shows how to find indexes and make insertions in a sorted list.