array='abcdcba'
array=list(array)
palindrome=[]
for x in range (0,len(array)):
list1 = []
list1.append(array[x])
for y in range(x+1,len(array)):
list1.append(array[y])
list2=list1[::-1]
if list1==list2:
palindrome.append(list1)
print(palindrome)
I'm trying to find the palindromes in the given string, and I'm experiencing weird behaviour.
When I find the palindrome bcdcb, I append the list to the palindrome array. Then when I append a new value to list1 in the next iteration, the value gets appended to the palindrome list too; making it bcdcba
The same happens to cdc; it prints it as cdcba.
The output I'm getting is:
[['a', 'b', 'c', 'd', 'c', 'b', 'a'], ['b', 'c', 'd', 'c', 'b', 'a'], ['c', 'd', 'c', 'b', 'a']]