python - Is there a built-in equivalent to C#'s Enumerable.Single()? -
in c#, if have enumerable , try call .single() on it, throw error if not have 1 element in it.
is there similar built-in python this?
if len(iterable) == 0 or len(iterable) > 1: raise error("...") return iterable[0]
not built in method, there idiomatic way achieve same goal:
(value,) = iterable
raises valueerror
if iterable
doesn't contain 1 element.
the single element stored in value
example simplified to:
(value,) = iterable return value
the unpacking feature of assignment operator.
if target list comma-separated list of targets: object must iterable same number of items there targets in target list, , items assigned, left right, corresponding targets.
Comments
Post a Comment