0

For example I have this array:

array = ['b2', 'jy', 'n3', 'ih', 'fc']

And I am trying to access each index of the array to check if is palindrome or not.

for i in range(len(array[i])):

    if array[i] == array[len(pal)-i-1]:
        booleano = "true"

    else:
        booleano = "false"


if booleano != "true":
    return false

else: return true

What am I missing?

3

1 Answer 1

3

You can check if string is palindrome using [::-1] slicing.

lst = ['bb', 'jhj', 'n3', 'ih', 'fc']
palindromes = [item == item[::-1] for item in lst]

Output

[True, True, False, False, False]
Sign up to request clarification or add additional context in comments.

3 Comments

[item == item[::-1]] for item in lst] might be closer to what they asked for, also maybe debug their current code too
Instead of that kind of output how can I have like: lst = ['bb true', 'jhj true', 'n3 false', 'ih false', 'fc false']
@AfonsoPinto, something like this: palindromes = [item + ' '+ str(item == item[::-1]) for item in lst]

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.