4 ms·
Anyone who's written a fair share of Python can immediately see "len(set(l)) == len(l)" as "no duplicate elements", this is such a standard way to check for dup
by aldanor 5y ago
Anyone who's written a fair share of Python can immediately see "len(set(l)) == len(l)" as "no duplicate elements", this is such a standard way to check for duplicates in Python that the chances are you've see this expression in this exact form dozens if not hundreds of times already.
Agreed with "l" identifier and docstring/comment. You'd probably write it like this if you want it to look nice:
from typing import Iterable, Any
def is_unique(elements: Iterable[Any]) -> bool:
"Returns True if the argument has no duplicate elements."
elements = list(elements)
return len(elements) == len(set(elements))
- 29athrowaway 5y agoI know Python but my Python is not the most Pythonic. The typing does make it more readable, but the "anyone who's written a fair share of Python can immediately see..." argument doesn't resonate with me. Naming boolean expressions saves time.
- koningrobot 5y agoThey did name the boolean expression; they named it `is_unique`. It's a poor name, but that's a different argument.