def _get_channel_to_group(channel_groups):
    """Take a list of ChannelGroup instances, and return a mapping
    channel ==> channel_group index"""
    mapping = {}
    channels_list = [(i, cg.channels) for i, cg in channel_groups.iteritems()]
    for igroup, channels in channels_list:
        for channel in channels:
            mapping[channel] = igroup
    return mapping