Going fast! #DataquestChallenge Premium Annual Offer:
500 get 50% & the next 1000 get 40% off.

# Why is my answer wrong?

My Code:

``````# Provided inputs
input_1 = "TAAT"
# Answer to this input: True

input_2 = "AGTTA"
# Answer to this input: False

input_3 = "T"
# Answer to this input: True
def is_palindrome(a):
a=list(a)
b=a
if a==b.reverse():
return True
else:
return False

``````

Hi! This does not work because the reverse() method reverses the list in place. So if we run the following code:

``````a= [1,2,3]
a.reverse()
a
``````

the output is [3,2,1]. The list a was reversed. But if we run the following:

``````a = [1,2,3]
print(a.reverse())
``````

the output is None.

To reverse the list a, you can use slicing: a[start,stop,steps] will return the slice of list a starting at index start, stopping at index stop and with step size steps. So a[::-1] will return the list a reversed.

Here is a version of your code that works:

``````def is_palindrome(a):
a=list(a)
b=a
if a==b[::-1]:
return True
else:
return False
``````

yes but here I am reversing b( in place ). So a should remain the same and b, which was copy of a, should be reversed. No?

No, because when you write b.reverse(), you are basically saying to python “reverse the list b”, but it does not return an object, it just reverses the list. That’s why when you print a.reverse in my example, it just shows None.
Just to convince you, you could run the following:

``````a = [1,2,3]
[3,2,1] == a.reverse()
``````

which will return False, as a.reverse() is not a list.

If you want to reverse b using the .reverse method, you could do this instead:

``````def is_palindrome(a):
a=list(a)
b=a.copy()
b.reverse()
if a==b:
return True
else:
return False
``````

Note that you have to use b=a.copy() here, otherwise writing b.reverse() will reverse both a and b, as they point to the same object.