networkx 2.8.2 Pypi GitHub Homepage
Other Docs
NotesParametersReturnsBackRef
to_dict_of_dicts(G, nodelist=None, edge_data=None)

Notes

For a more custom approach to handling edge data, try:

dod = {
    n: {
        nbr: custom(n, nbr, dd) for nbr, dd in nbrdict.items()
    }
    for n, nbrdict in G.adj.items()
}

where custom returns the desired edge data for each edge between n and :None:None:`nbr`, given existing edge data :None:None:`dd`.

Parameters

G : graph

A NetworkX graph

nodelist : list

Use only nodes specified in nodelist

edge_data : scalar, optional

If provided, the value of the dictionary will be set to :None:None:`edge_data` for all edges. Usual values could be :None:None:`1` or :None:None:`True`. If :None:None:`edge_data` is :None:None:`None` (the default), the edgedata in G is used, resulting in a dict-of-dict-of-dicts. If G is a MultiGraph, the result will be a dict-of-dict-of-dict-of-dicts. See Notes for an approach to customize handling edge data. :None:None:`edge_data` should not be a container.

Returns

dod : dict

A nested dictionary representation of G. Note that the level of nesting depends on the type of G and the value of :None:None:`edge_data` (see Examples).

Returns adjacency representation of graph as a dictionary of dictionaries.

See Also

from_dict_of_dicts
to_dict_of_lists

Examples

>>> G = nx.path_graph(3)
... nx.to_dict_of_dicts(G) {0: {1: {}}, 1: {0: {}, 2: {}}, 2: {1: {}}}

Edge data is preserved by default ( edge_data=None ), resulting in dict-of-dict-of-dicts where the innermost dictionary contains the edge data:

>>> G = nx.Graph()
... G.add_edges_from(
...  [
...  (0, 1, {'weight': 1.0}),
...  (1, 2, {'weight': 2.0}),
...  (2, 0, {'weight': 1.0}),
...  ]
... )
... d = nx.to_dict_of_dicts(G)
... d # doctest: +SKIP {0: {1: {'weight': 1.0}, 2: {'weight': 1.0}}, 1: {0: {'weight': 1.0}, 2: {'weight': 2.0}}, 2: {1: {'weight': 2.0}, 0: {'weight': 1.0}}}
>>> d[1][2]['weight']
2.0

If :None:None:`edge_data` is not :None:None:`None`, edge data in the original graph (if any) is replaced:

>>> d = nx.to_dict_of_dicts(G, edge_data=1)
... d {0: {1: 1, 2: 1}, 1: {0: 1, 2: 1}, 2: {1: 1, 0: 1}}
>>> d[1][2]
1

This also applies to MultiGraphs: edge data is preserved by default:

>>> G = nx.MultiGraph()
... G.add_edge(0, 1, key='a', weight=1.0) 'a'
>>> G.add_edge(0, 1, key='b', weight=5.0)
'b'
>>> d = nx.to_dict_of_dicts(G)
... d # doctest: +SKIP {0: {1: {'a': {'weight': 1.0}, 'b': {'weight': 5.0}}}, 1: {0: {'a': {'weight': 1.0}, 'b': {'weight': 5.0}}}}
>>> d[0][1]['b']['weight']
5.0

But multi edge data is lost if :None:None:`edge_data` is not :None:None:`None`:

>>> d = nx.to_dict_of_dicts(G, edge_data=10)
... d {0: {1: 10}, 1: {0: 10}}
See :

Back References

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

networkx.linalg.graphmatrix.adjacency_matrix networkx.convert.to_dict_of_dicts

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/convert.py#243
type: <class 'function'>
Commit: