words = ['Anna', 'Alexey', 'Alla', 'Kazak', 'Dom']
def palindrome():
filtered_2 = list(filter(lambda x: x == x[::-1], words))
print(filtered_2)
palindrome()
The code should return palindrome words, but it gives me an output like this:
[]
It should give:
['Anna', 'Alla', 'Kazak']
>Solution :
You need to normalize the input before comparisons. Basically you are checking if 'A' == 'a' which will fail.
Try this example where I normalize to lowercase using str.lower():
words = ['Anna', 'alexey', 'Alla', 'kazak', 'dom']
def palindrome():
filtered_2 = list(filter(lambda x: x.lower() == x.lower()[::-1], words))
print(filtered_2)
palindrome()