def collapse_addresses(addresses):
i = <NUM_LIT:0><EOL>addrs = []<EOL>ips = []<EOL>nets = []<EOL>for ip in addresses:<EOL><INDENT>if isinstance(ip, _BaseAddress):<EOL><INDENT>if ips and ips[-<NUM_LIT:1>]._version != ip._version:<EOL><INDENT>raise TypeError("<STR_LIT>" % (<EOL>ip, ips[-<NUM_LIT:1>]))<EOL><DEDENT>ips.append(ip)<EOL><DEDENT>elif ip._prefixlen == ip._max_prefixlen:<EOL><INDENT>if ips and ips[-<NUM_LIT:1>]._version != ip._version:<EOL><INDENT>raise TypeError("<STR_LIT>" % (<EOL>ip, ips[-<NUM_LIT:1>]))<EOL><DEDENT>try:<EOL><INDENT>ips.append(ip.ip)<EOL><DEDENT>except AttributeError:<EOL><INDENT>ips.append(ip.network_address)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if nets and nets[-<NUM_LIT:1>]._version != ip._version:<EOL><INDENT>raise TypeError("<STR_LIT>" % (<EOL>ip, nets[-<NUM_LIT:1>]))<EOL><DEDENT>nets.append(ip)<EOL><DEDENT><DEDENT>ips = sorted(set(ips))<EOL>nets = sorted(set(nets))<EOL>while i < len(ips):<EOL><INDENT>(first, last) = _find_address_range(ips[i:])<EOL>i = ips.index(last) + <NUM_LIT:1><EOL>addrs.extend(summarize_address_range(first, last))<EOL><DEDENT>return iter(_collapse_addresses_recursive(sorted(<EOL>addrs + nets, key=_BaseNetwork._get_networks_key)))<EOL>
Collapse a list of IP objects.

    Example:
        collapse_addresses([IPv4Network('192.0.2.0/25'),
                            IPv4Network('192.0.2.128/25')]) ->
                           [IPv4Network('192.0.2.0/24')]

    Args:
        addresses: An iterator of IPv4Network or IPv6Network objects.

    Returns:
        An iterator of the collapsed IPv(4|6)Network objects.

    Raises:
        TypeError: If passed a list of mixed version objects.