def collapse_addresses(addresses):
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>if ips:<EOL><INDENT>for first, last in _find_address_range(ips):<EOL><INDENT>addrs.extend(summarize_address_range(first, last))<EOL><DEDENT><DEDENT>return _collapse_addresses_internal(addrs + nets)<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.