def assert_single_element ( iterable ) : it = iter ( iterable ) first_item = next ( it ) try : next ( it ) except StopIteration : return first_item raise ValueError ( "iterable {!r} has more than one element." . format ( iterable ) )
python iterator returns triplet i want just one of the elements
Get the single element of iterable or raise an error .
def assert_single_element(iterable):
  """Get the single element of `iterable`, or raise an error.

  :raise: :class:`StopIteration` if there is no element.
  :raise: :class:`ValueError` if there is more than one element.
  """
  it = iter(iterable)
  first_item = next(it)

  try:
    next(it)
  except StopIteration:
    return first_item

  raise ValueError("iterable {!r} has more than one element.".format(iterable))