2

I'm writing a program to identify palindromes using Python, using lists. However, my program always states that the input word is a palindrome, even if it clearly isn't


word = input("Type a word, and I'll tell you if it's a palidrome or not: ")
word = " ".join(word)

new_word = word.split() #forms a list from user-inputted word
print(new_word)

variable = new_word
variable.reverse() #reverses word list 
print(variable)

if variable == new_word:
    print("This is a palidrome")
else:
    print("This is not a palidrome")

4 Answers4

1

variable is a shallow copy of the new_word list, so variable is also reversed (as it refers to the same list) . Try using

variable = copy.deepcopy(new_word)
mrtnlrsn
  • 1,105
  • 11
  • 19
1

You can directly get the result by reversing the input string also, Use the below code:-

word = input("Type a word, and I'll tell you if it's a palidrome or not: ")
new_word = list(reversed(word))  #Reversing the string
new_word = ''.join(new_word)  # Converting list into string

if word == new_word :
    print("This is a palidrome")
else:
    print("This is not a palidrome") 

OR

I have made changes in your code:-

word = input("Type a word, and I'll tell you if it's a palidrome or not: ")
word = " ".join(word)

new_word = word.split() #forms a list from user-inputted word
print(new_word)

variable = new_word.copy()   # This is the change I have made.
variable.reverse() #reverses word list 
print(variable)

if variable == new_word:
    print("This is a palidrome")
else:
    print("This is not a palidrome")

I hope it may help you.

Rahul charan
  • 765
  • 7
  • 15
  • Can you elaborate on why this works while the original one does not? – Lucas Wieloch Jul 11 '19 at 10:10
  • @LucasWieloch `variable = new_word variable.reverse()` when you are making changes in the `variable` it is also making changes in `new_word`. Instead of this you have to use 'variable=new_word.copy()` ,Now you can make changes in `variable` by `variable.reverse()`, it will not make changes in `new_word`. – Rahul charan Jul 11 '19 at 10:17
  • Thanks, I initially did the former which is nice and concise – Jeremy Feng Jul 11 '19 at 10:46
1

The reason that variable == new_word is always true is that the assignment operator in this case merely creates a new pointer, not a new list.

In other words, variable = new_word does not make a copy of the list -- it makes variable point to the same list in memory. So when you reverse variable, you are actually reversing the original list. You can see this if you print new_word after you have run variable.reverse().

This article is a helpful introduction to pointers, and this one is a nice explanation of assignment vs. shallow-copy vs. deep-copy. Since your list is just a list of strings, shallow-copy will do the trick.[1] Deep-copy is overkill, but it works too.

Shallow-copy:

variable = list(new_word)

For Python 3.3 and later, lists have a built-in copy method:

variable = new_word.copy()

Another option is to use slicing, but without supplying a starting or ending index:

variable = new_word[:]

Finally, the copy module supplies a function for creating shallow copies:

variable = copy.copy(new_word)

Deep-copy:

import copy
variable = copy.deepcopy(new_word)

[1] While mrtnlrsn says you have made a shallow copy, this is not true, as the linked articles explains.

Community
  • 1
  • 1
jlcope
  • 34
  • 6
0

you need to replace

variable = new_word

with

variable = new_word[:]

This will make a proper copy of your variable, that you can manipulate independently.

pygri
  • 647
  • 6
  • 17