Are all items in list the same?

Skip Montanaro skip.montanaro at gmail.com
Mon Jan 7 21:01:39 EST 2019


>
> >      if len(a) == 0 or all(i == a[0] for i in a[1:]):
> >
> You don't need to check the length of the list because if the list is
> empty, 'all' will return True anyway.
>

Given the structure of the expression passed as an argument to all(), won't
you get an IndexError if a is empty without the guard?

(Would try it out if I had an interpreter on my phone...)

S



More information about the Python-list mailing list