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?

  • What exactly is wrong with the code you show? Read and follow [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Rory Daulton May 17 '18 at 13:57
  • 1
    [How to debug small programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs) – akshat May 17 '18 at 13:59
  • This can also be useful to verify conditions in a list: http://www.secnetix.de/olli/Python/list_comprehensions.hawk – Jundiaius May 17 '18 at 14:00

1 Answers1

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]
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128