networkx.utils.arbitrary_element

networkx.utils.arbitrary_element(iterable)[source]

Returns an arbitrary element of iterable without removing it.

This is most useful for “peeking” at an arbitrary element of a set, but can be used for any list, dictionary, etc., as well:

>>> arbitrary_element({3, 2, 1})
1
>>> arbitrary_element('hello')
'h'

This function raises a ValueError if iterable is an iterator (because the current implementation of this function would consume an element from the iterator):

>>> iterator = iter([1, 2, 3])
>>> arbitrary_element(iterator)
Traceback (most recent call last):
    ...
ValueError: cannot return an arbitrary item from an iterator