Are all items in list the same?

Bob van der Poel bob at mellowood.ca
Tue Jan 8 11:54:14 EST 2019


Thanks guys for the help on this. As it turns out I have learned new
commands as a result of the question: all() and any() will work perfectly!
The other solutions, as always, are enlightening. And, no, Chris, this is
not homework :)

On Tue, Jan 8, 2019 at 9:31 AM Neil Cerutti <neilc at norwich.edu> wrote:

> On 2019-01-08, MRAB <python at mrabarnett.plus.com> wrote:
> > On 2019-01-08 00:47, ike at koeln.ccc.de wrote:
> >> You might do something like
> >>
> >>      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.
>
> Neat! I expected that a[0] would be executed in that case,
> but it is not.
>
> --
> Neil Cerutti
> --
> https://mail.python.org/mailman/listinfo/python-list
>


-- 

**** Listen to my FREE CD at http://www.mellowood.ca/music/cedars ****
Bob van der Poel ** Wynndel, British Columbia, CANADA **
EMAIL: bob at mellowood.ca
WWW:   http://www.mellowood.ca



More information about the Python-list mailing list