palindrome function

Hugh M hyoogle at gmail.com
Fri Jul 11 20:34:02 EDT 2008


> Basically, it reverses the list in place, so it modifies the list which
> called it. It does not return a /new/ list which is a reversed version of
> the original, as you expected it to. Since it doesn't return anything
> explicitly, Python makes it return None. Hence, the comparison you are doing
> is between the original list and a None, which is False, naturally.
> Try this:
>
> spam = ['a', 'n', 'n', 'a']
> eggs = spam[:]
> if spam.reverse() == eggs:
>   print "Palindrome"
>
>
Um, wouldn't this suffer the same problem- spam.reverse() would return None,
so None==eggs test would return false?

I think you meant to say:

spam = ['a', 'n', 'n', 'a']
eggs = spam[:]
spam.reverse()
if spam == eggs:
  print "Palindrome"

-Hugh


> --
> Denis Kasak
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080711/5316978f/attachment-0001.html>


More information about the Python-list mailing list