I am doing some graph theory and I use the NetworkX library.
In some algorithms, one needs to get an arbitrary item from a dictionary to start it (for instance, one of the neighbors of a given node, and the neighborhoods are a dict in NetworkX).
By arbitrary element I mean any element, I don't care which one (not a given element, not a random element, etc.)
Currently I do the following:
D = {i:i**2 for i in range(N)} # an example of a dictionary where N is a positive integer
any_item_of_D = list(D)[0] # getting a item of D
But it appears that the time complexity of any_item_of_D = list(D)[0]
is linearly growing as N tends to infinity (a O(N) complexity).
Is it possible to get an arbitrary item from a dictionary with a O(1) complexity in Python?