palindrome function

Denis Kasak denis.kasak2718281828 at gmail.com
Sat Jul 12 03:13:05 EDT 2008


Peter Otten wrote:
> Denis Kasak wrote:
> 
>> 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"
> 
> Your explanation is correct, but your example code compares None to
> ['a', 'n', 'n', 'a'] and therefore won't print "Palindrome", either.

Of course. Thank you for the correction. I guess you know your caffeine 
has started to wear off when you start making the same mistakes you were 
trying to fix. :-)

-- 
Denis Kasak



More information about the Python-list mailing list