1

I have a recursive function with a dictionary as an argument, the function changes the dictionary before it calls itself. I would like the change of the dictionary to only affect the called function and nothing outside the of the scope.

Can I create a local dictionary?

dict = {10 : 5, 20 : 5}

def recursive_func(dict_arg, total_arg):
    dict_local = dict_arg

    for k, v in dict_local.items():
        total = total_arg + k
        if total >= 25:
            dict_local.update({k : v -1})
            print(str(total) + str(dict_local))
        else:
            dict_local.update({k : v -1})
            recursive_func(dict_local, total)

print("dict before func: " + str(dict))
recursive_func(dict, 0)
print("dict after func: " + str(dict))


terminal >>>
dict before func: {10: 5, 20: 5}
30{10: 2, 20: 5}
40{10: 2, 20: 4}
30{10: 2, 20: 3}
30{10: 1, 20: 2}
40{10: 1, 20: 1}
dict after func: {10: 1, 20: 1}

As can be seen the dictionary is changed after the function and the printed total does not match with the coresponding amount of 10 and 20's left in the dictionary.

Floxxy
  • 7
  • 3

3 Answers3

0

Check out the copy() operator. You probably don't need deepcopy() here.

https://docs.python.org/2/library/copy.html

You can use the following code:

test_dict = {10 : 5, 20 : 5}

def recursive_func(dict_arg, total_arg):
    dict_local = test_dict.copy()

    for k, v in dict_local.items():
        total = total_arg + k
        if total >= 25:
            dict_local.update({k : v -1})
            print(str(total) + str(dict_local))
        else:
            dict_local.update({k : v -1})
            recursive_func(dict_local, total)

print("dict before func: ", str(test_dict))
recursive_func(test_dict, 0)
print("dict after func: ", str(test_dict))

Produces:

dict before func:  {10: 5, 20: 5}
30{10: 4, 20: 5}
40{10: 4, 20: 4}
30{10: 4, 20: 4}
30{10: 4, 20: 5}
40{10: 4, 20: 4}
dict after func:  {10: 5, 20: 5}
artemis
  • 6,857
  • 11
  • 46
  • 99
0

You need to make a deep copy of your dict, otherwise it will only be referenced, always changing the original dict with every change on the copy.

import copy

dict_local = copy.deepcopy(dict_arg)

This should work. As Jerry M. pointed out you probably don't need a deepcopy however,since your values of the dict are only ints. For difference between copy and deepcopy see this for example.

LeoE
  • 2,054
  • 1
  • 11
  • 29
-1
dict = {10 : 5, 20 : 5}

def recursive_func(dict_arg, total_arg):

    for k, v in dict_arg.items():
        dict_local = dict_arg.copy()
        total = total_arg + k
        if total >= 25:
            dict_local.update({k : v -1})
            print(str(total) + str(dict_local))
        else:
            dict_local.update({k : v -1})
            recursive_func(dict_local, total)

print("dict before func: ", str(dict))
recursive_func(dict, 0)
print("dict after func: ", str(dict))


>>>
dict before func:  {10: 5, 20: 5}
30{10: 2, 20: 5}
40{10: 3, 20: 4}
30{10: 4, 20: 4}
30{10: 4, 20: 4}
40{10: 5, 20: 3}
dict after func:  {10: 5, 20: 5}

Floxxy
  • 7
  • 3
  • This is just a copy of the code I posted. If what I posted solved your problem, please mark it as correct for others to use in the future :) @Floxxy – artemis Oct 03 '19 at 03:28