This is the information I'm using: customers = [1, 2, 3] customer_locations = { 1: [[1], [4], [1,4]], 2: [[2], [5], [2,5]], 3: [[3], [6], [3,6]]} First, I pass the dict history_actual to the function tsp_dinamico then I have to save the cost of that dict in another dic, so I transform the dic to a tuple using function dict_to_tuple_key, but in the next iteration when passing history_actual to the function tsp_dinamico as it expect a dict an error shows up. This is the error: Cell In[7], line 27 24 # Restricciones 25 # Cada conjunto de clientes debe ser visitado exactamente una vez 26 for c in new_customers: ---> 27 model.addConstr(gp.quicksum(y for i in new_c_locations[c]) == 1, f"Customer_{c}") 29 # Conservación de flujo en los nodos internos 30 for i in data['locations']: KeyError: (1,) This is the typical form of new_c_locations (history_actual) {1: [1, 4], 2: [2], 3: [3]} it looks like instead of a list now the dict has tuples. I tried with deep copy but it did not work. for i in range(1, N+1): with open(f"{Folder}\\instance_base{i}.pkl", 'rb') as f: data_dummy = pickle.load(f) V = {} for t in reversed(customers): V_t = {} if t == max(customers): prev_customers = customers[:-1] all_histories = list(product(*[customer_locations[c] for c in prev_customers])) else: all_histories = V[t+1].keys() for history in all_histories: base_history = {c: loc for c, loc in zip(prev_customers, history)} for location_t in customer_locations[t]: # Create a deep copy to not modify original dic history_actual = base_history.copy() history_actual[t] = location_t tour, tcost, _, _ = tsp_dinamico(data_dummy, list(history_actual.keys()), history_actual, InitialSolution=True) # Convert dic to tuple to use as key in V history_tuple = dict_to_tuple_key(copy.deepcopy(history_actual)) if history_tuple not in V_t or tcost < V_t[history_tuple]: V_t[history_tuple] = tcost # Save values in V V[t] = V_t best_solution = min(V[1], key=V[1].get) best_cost = V[1][best_solution] I need to find a way of passing the dict history_actual to the tsp_dinamico and then transform it to a tuple but keeping it as a dict for the next iteration. I hope this is now a bit clear. Any help will be appreciated. Continue reading...