On Thu, Oct 06, 2016 at 04:45:01PM +0300, Filipp Bakanov wrote:
> For now there are many usefull builtin functions like "any", "all", etc.
> I'd like to propose a new builtin function "equal". It should accept
> iterable, and return True if all items in iterable are the same or iterable
> is emty.
> That's quite popular problem, there is a discussion of how to perform it on
> stackoverflow (
> http://stackoverflow.com/questions/3844801/check-if-all-elements-in-a-list-are-identical)
> - all suggestions are either slow or not very elegant.

I haven't checked the link, but just off the top of my head, how's this?

def all_equal(iterable):
    it = iter(iterable)
    sentinel = object()
    first = next(it, sentinel)
    return all(x == first for x in it)


I think that's neat, elegant, fast, and short enough that I don't mind 
writing it myself when I need it (although I wouldn't mind adding it to 
my own personal toolbox).

+0.3 to adding it the standard library.

+0.1 to adding it to built-ins

-0.1 on adding it to built-ins under the name "equal", as that will 
confuse too many people.


-- 
Steve
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to