Python: Copy two dependent lists together with their dependence
Question: I am stuck with some problem which I guess is not very difficult, but I could
not find any answer to it.

I have two lists of objects, each of them containing lists of objects in the
other. I would like to copy them both to do come tests and evaluate the
results before repeating the process. In the end, I would keep the best
result.

However, when copying each lists, the result is, unsurprisingly, not two
dependent lists but two lists which do not interact anymore. How can I solve
this? Is there some proper way to do it?

Given the two classes defined as follow.

    
    
    import copy
    
    class event:
    
        def __init__(self, name):
            self.name = name
            self.list_of_persons = []
    
        def invite_someone(self, person):
            self.list_of_persons.append(person)
            person.list_of_events.append(self)
    
    class person:
    
        def __init__(self, name):
            self.name = name
            self.list_of_events = []
    

I tried to write some simple example of the situation I am facing. The print
function shows that the objects identifiers are different in the two lists.

    
    
    # Create lists of the events and the persons
    the_events = [event("a"), event("b")]
    the_persons = [person("x"), person("y"), person("z")]
    
    # Add some persons at the events
    
    the_events[0].invite_someone(the_persons[0])
    the_events[0].invite_someone(the_persons[1])
    
    the_events[1].invite_someone(the_persons[1])
    the_events[1].invite_someone(the_persons[2])
    
    
    print("Original :", id(the_persons[1]), id(the_events[0].list_of_persons[1]), id(the_events[1].list_of_persons[0]))
    
    
    # Save the original configuration
    original_of_the_events = copy.deepcopy(the_events)
    original_of_the_persons = copy.deepcopy(the_persons)
    
    for i in range(10):
    
        # QUESTION: How to make the following copies?
        the_events = copy.deepcopy(original_of_the_events)
        the_persons = copy.deepcopy(original_of_the_persons)
    
        print("   i =", i, ":", id(the_persons[1]), id(the_events[0].list_of_persons[1]), id(the_events[1].list_of_persons[0]))
    
        # Do some random stuff with the two lists
        # Rate the resulting lists
        # Record the best configuration
    
    # Save the best result in a file
    

I thought about using some dictionary and make the list independent, but that
would imply a lot of code revision which I would like to avoid.

Thank you in advance for any help! I am new both to Python and StackExchange.


Answer: Since deepcopy makes copies of all underlying objects of the thing being
copied, doing two independent calls to deepcopy breaks your links between
objects. If you create a new object with references to both of these things
(like a dict) and copy that object, that will preserve the object references.

    
    
    workspace = {'the_persons': the_persons, 'the_events': the_events}
    cpw = copy.deepcopy(workspace)
    

