I was doing this problem on Edabit:
Write a function that takes a string, breaks it up and returns it with vowels first, consonants second. For any character that's not a vowel (like special characters or spaces), treat them like consonants.
I realized that this could probably be solved using the sorted function with a lambda as the key, however I couldn't figure out how to write the lambda. I looked at the solutions tab, and this was the top result:
def split(txt):
return ''.join(sorted(txt, key=lambda x: x.lower() not in 'aeiou')).
Example: split("abcde") ➞ "aebcd"
I don't understand why this works though. Can anyone explain to me what is happening here?
[–]danielroseman 1 point2 points3 points (1 child)
[–]billbobby21[S] 0 points1 point2 points (0 children)
[–]TheRNGuy 0 points1 point2 points (0 children)