def connect_caves(world, lw_entrances, dw_entrances, caves, player):
    'This works inplace'
    random.shuffle(lw_entrances)
    random.shuffle(dw_entrances)
    random.shuffle(caves)
    while caves:
        cave_candidate = (None, 0)
        for (i, cave) in enumerate(caves):
            if isinstance(cave, str):
                cave = (cave,)
            if (len(cave) > cave_candidate[1]):
                cave_candidate = (i, len(cave))
        cave = caves.pop(cave_candidate[0])
        target = (lw_entrances if (random.randint(0, 1) == 0) else dw_entrances)
        if isinstance(cave, str):
            cave = (cave,)
        if (len(target) < len(cave)):
            target = (lw_entrances if (target is dw_entrances) else dw_entrances)
        for exit in cave:
            connect_two_way(world, target.pop(), exit, player)
This works inplace
def connect_caves(world, lw_entrances, dw_entrances, caves, player):
    
    random.shuffle(lw_entrances)
    random.shuffle(dw_entrances)
    random.shuffle(caves)
    while caves:
        cave_candidate = (None, 0)
        for (i, cave) in enumerate(caves):
            if isinstance(cave, str):
                cave = (cave,)
            if (len(cave) > cave_candidate[1]):
                cave_candidate = (i, len(cave))
        cave = caves.pop(cave_candidate[0])
        target = (lw_entrances if (random.randint(0, 1) == 0) else dw_entrances)
        if isinstance(cave, str):
            cave = (cave,)
        if (len(target) < len(cave)):
            target = (lw_entrances if (target is dw_entrances) else dw_entrances)
        for exit in cave:
            connect_two_way(world, target.pop(), exit, player)