I'm wondering if there is a way to write such a function with use of single list comprehension:
def sort_letters(word):
lowers, uppers = [], []
for letter in word:
lowers.append(letter) if letter.islower() else uppers.append(letter)
return lowers,uppers
I can write it like that:
def sort_letters2(word):
return [x for x in word if x.islower()],[x for x in word if x.isupper()]
but it iterates twice through word. I'm curious how to sort letters within single list comprehension to get result similar to:
sort_letters("HaLLo")
(['a', 'o'], ['H', 'L', 'L'])
It doesn't necessary needs to look exactly like result up, but it should stick to idea of sorting lower case letters from upper. Is there way to write it like that?