Graph.nodes() and Graph.edges() changed from lists to views in networkx 2.0
28K reputation · 18 Jun 2022, 20:12 UTC
When upgrading a project from networkx 1.x to version 2.0 or later, the goal is to keep existing code that calls G.nodes() or G.edges() functioning without modification, even though these methods now return view objects instead of plain lists.
The constraint is that any reliance on list‑specific operations—such as indexing, slicing, or calling len() directly on the returned object—will raise TypeError or produce unexpected results, while wrapping every call with list() restores the old behavior but may introduce noticeable performance penalties on large graphs.
How can developers efficiently identify the locations that truly need list conversion? What trade‑offs exist between selective list() wrappers and a global compatibility shim? Is there a supported way to configure networkx to revert to the legacy list return type for specific graphs or sessions?