networkx 2.8.2 Pypi GitHub Homepage
Other Docs
NotesParametersRaisesReturnsBackRef
arbitrary_element(iterable)

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

Notes

This function does not return a random element. If iterable is ordered, sequential calls will return the same value:

>>> l = [1, 2, 3]
>>> nx.utils.arbitrary_element(l)
1
>>> nx.utils.arbitrary_element(l)
1

Parameters

iterable : `abc.collections.Iterable` instance

Any object that implements __iter__ , e.g. set, dict, list, tuple, etc.

Raises

ValueError

If iterable is an iterator (because the current implementation of this function would consume an element from the iterator).

Returns

The object that results from ``next(iter(iterable))``

Returns an arbitrary element of iterable without removing it.

Examples

Arbitrary elements from common Iterable objects:

>>> nx.utils.arbitrary_element([1, 2, 3])  # list
1
>>> nx.utils.arbitrary_element((1, 2, 3))  # tuple
1
>>> nx.utils.arbitrary_element({1, 2, 3})  # set
1
>>> d = {k: v for k, v in zip([1, 2, 3], [3, 2, 1])}
... nx.utils.arbitrary_element(d) # dict_keys 1
>>> nx.utils.arbitrary_element(d.values())   # dict values
3

:None:None:`str` is also an Iterable:

>>> nx.utils.arbitrary_element("hello")
'h'

ValueError is raised if iterable is an iterator:

This example is valid syntax, but raise an exception at execution
>>> iterator = iter([1, 2, 3])  # Iterator, *not* Iterable
... nx.utils.arbitrary_element(iterator) Traceback (most recent call last): ... ValueError: cannot return an arbitrary item from an iterator
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

networkx.utils.misc.arbitrary_element

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


GitHub : /networkx/utils/misc.py#320
type: <class 'function'>
Commit: