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

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -