Are all items in list the same?

Cameron Simpson cs at cskk.id.au
Tue Jan 8 17:44:45 EST 2019


On 08Jan2019 15:28, Dan Sommers <2QdxY4RzWzUUiLuE at potatochowder.com> wrote:
>    >>> a = [1, 1, 1, 1, 1]
>    >>> a[1:] == a[:-1]
>    True
>    >>> a == a[::-1]
>    True
>
>    >>> a = [1, 2, 3, 4, 3, 2, 1]
>    >>> a[1:] == a[:-1]
>    False
>    >>> a == a[::-1]
>    True
>
>Looks like Peter's pretty clever after all.  :-)

Except that his solution always scans then entire list. Twice.

For large dissimilar lists this gets needlessly expensive in a linear 
fashion with the length of the list.

It is succinct, but wasteful.

Cheers,
Cameron Simpson <cs at cskk.id.au>



More information about the Python-list mailing list