This code is more efficient because every function involved here is "lazy": they doesdo only as much work as they need to. reversed
, zip
, islice
, and the generator expression all return an iterator that can produce elements (but do little work up front). all
also exits as soon as it gets a Falsey result, so it's comparable to a for
loop that contains a break
in some branches. This is key here because we only want to do as much work as is necessary to determine whether not they're palindromes. Making two full copies of the list does a large amount of work; far more than is required to check if the string is a palindrome.