1

Here is the question I am trying to answer:

Write code that accepts a list of strings as its parameter and indicates whether that list is a palindrome. A palindrome list is one that reads the same forward as backward. For example, the list ["alpha", "beta", "gamma", "delta", "gamma", "beta", "alpha"] is a palindrome.

def palindrome_checker(list):
    if list == list.reverse():
        print('Palindrome')
    else:
        print('Not Palindrome')


user_list = []
user_input = (input('Enter list of words'))
user_list = [user_input.split()]
palindrome_checker(user_list)

My code responds "Not Palindrome" no matter what. Can anyone tell me what I am missing?w

ewokx
  • 2,204
  • 3
  • 14
  • 27
tusky
  • 37
  • 2

4 Answers4

1

reverse is an inplace function i.e it returns None. So a list is never equal to None. Also, .split() itself provides a list. [user_input.split()] will create a nested list.

So a solution.

  1. Remove the [] from .split()
  2. Create a copy of the list before you reverse it.
def palindrome_checker(lists):
    lists2=lists[:]
    lists.reverse()
    if lists2 == lists:
        print('Palindrome')
    else:
        print('Not Palindrome')


user_list = []
user_input = (input('Enter list of words'))
user_list = user_input.split()
palindrome_checker(user_list)
  • 4
    You cannot do `list2=lists` since this will just create a pointer to the same list. Then when you do `lists.reverse()`, both lists will be reversed. You should do `list2 = lists[:]` to create a copy. – TYZ Aug 11 '21 at 02:10
  • @tusky most welcome. Consider accepting the answer –  Aug 11 '21 at 02:17
  • 1
    Not to be overly picky but the parameter name `lists` suggests that the method is expecting a list of multiple lists – PreciseMotion Aug 11 '21 at 02:32
1

list.reverse() does not return the reversed version of the list, it actually doesn't return anything at all. The reverse function actually changes the original list object.

So basically, you're saying if list == None in that statement.

Does that make sense?

You can generally assume anytime you call a method that belongs to an object, for example in this case, the reverse() method in the List object, that the method is going to change the original object, not return a new value.

What you want is:

reversedList = list(reversed(original_list))
if original_list = reversed_list:

On another note, I suggest you rename your parameter from list to something else for the sake of cleanliness. You want to avoid namespace collisions since the word list already refers to the type list and the method list that constructs a list.

PreciseMotion
  • 330
  • 1
  • 14
1

Aside from the answers already here, you can also do:

my_list == list(reversed(my_list))

or

list == list[::-1]

EDIT:

First one will fail because of conflicting names on list, since it's a reserved keyword built-in function. I suggest change the name of your variable as well to something different if you choose the first approach

Brian Destura
  • 11,487
  • 3
  • 18
  • 34
  • 1
    I was about to provide the same answer, so I added a [small variation to yours](https://stackoverflow.com/a/68735274/16343464) to get the text output. +1 as this is the most pythonic in my opinion – mozway Aug 11 '21 at 02:18
  • ```list``` is an in built function –  Aug 11 '21 at 02:24
  • The term may be inexact but the practice is prone to error and should be avoided – mozway Aug 11 '21 at 02:25
1

For fun a variation on @bdbd answer:

def palindrome_checker(l):
    return ('' if l == list(reversed(l)) else 'Not ') + 'Palindrome'
Brian Destura
  • 11,487
  • 3
  • 18
  • 34
mozway
  • 194,879
  • 13
  • 39
  • 75