Search code examples
pythonpalindrome

Palindrome case


I'm writing a function to check whether the integer is palindrome on Leetcode. Most of cases are acceptable. But there is a special case which is 10, why the output of my code return True. The idea for my code is convert integer to str and then convert it to list. Then reverse the list and compare two lists whether are the same.

Is my idea or code does not work out right or there are sth I missed, would be appreciate if someone can point out. Here is the code screenshot


initial_list=list(str(x))

temp = initial_list

initial_list.reverse()

if temp == initial_list:
    print('True')
else:
    print('False')

Solution

  • To create a copy of your initial list, use: temp = initial_list.copy()

    Otherwise, temp is just a reference to the original initial_list.

    Try printing temp again after applying initial_list.reverse()