My goal is to take a string and find the first occurance of each alphabetical letter and append it to a string. You would remove those letters from the original string and repeat until the original string is empty. For example, "aabdc" should return "abcad". To do this I split my string into a list and go through it checking for each letter. However, I keep getting an index out of range error because my for i in range(len(string)) keeps changing after I remove certain characters from the string. How would I fix this?
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
def split(word):
return list(word)
def order(s):
output = ""
x = 0
s = split(s)
for i in range(len(s)):
if alphabet[x] not in s:
x += 1
if alphabet[x] == s[i]:
output += s[i]
print(output)
s.remove(s[i])
print(s)
x += 1
return(output)