Your "clusters" are typically called components in network analysis.
In networkx, you can compute them using the function nx.connected_components, which returns an iterator over components. Each component returned by the iterator simply a list of nodes.
import networkx as nx
g = nx.Graph()
... # add nodes & edges
components = nx.connected_components(g)
for component in components:
if len(components) == 2:
# do something special with components of size 2
If you are only interested in the largest component (often called "giant component"), it is worth noting that you can then sort the components or find the largest group by post-processing this output as detailed in the documentation:
Generate a sorted list of connected components, largest first.
>>> G = nx.path_graph(4)
>>> nx.add_path(G, [10, 11, 12])
>>> [len(c) for c in sorted(nx.connected_components(G), key=len, reverse=True)]
[4, 3]
If you only want the largest connected component, it's more
efficient to use max instead of sort.
>>> largest_cc = max(nx.connected_components(G), key=len)