Use ipaddress module,handles IPv4 and IPv6 addresses.
If still need to do it there is
>>> import ipaddress >>> ip_1 = ipaddress.ip_address('fc00::1') >>> ip_2 = ipaddress.ip_address('fc00:0:2::1') >>> ip_1 < ip_2 TrueAddress and Network objects are not sortable by default.
If still need to do it there is
ipaddress.get_mixed_type_key
that be used as a key in sorted()
.import ipaddress >>> ip_1 = ipaddress.ip_address('fc00::9') >>> ip_2 = ipaddress.ip_address('fc00::5') >>> ip_3 = ipaddress.ip_address('fc00::3') >>> ip_4 = ipaddress.ip_address('fc00::100') >>> sorted([ip_1, ip_2, ip_3, ip_4], key=ipaddress.get_mixed_type_key) [IPv6Address('fc00::3'), IPv6Address('fc00::5'), IPv6Address('fc00::9'), IPv6Address('fc00::100')]