0

I have a json file from which the Python script reads key and value pairs and stores in notes class variable. One of the key in notes is called project_suites. I am trying to duplicate this project_suites key multiple times depending on sub-directories count in the script location. For each duplicate, I am including input_path and output_path:

# project_folders contains list of sub-directories            
for idx, path in enumerate(project_folders):
    # relative_path gets path without the test_path
    relative_path = os.path.relpath(path, test_path)

    # split the relative_path into a list
    path_list     = os.path.normpath(relative_path).split(os.path.sep)

    # create new sub-key and assign path_list
    notes.values['project_suites'][idx]['input_path'] = path_list
    notes.values['project_suites'][idx]['output_path'] = path_list
    print(notes.values['project_suites'][idx])

This seems to be working with the above print in the loop outputs the following:

{'recursive': True, 'project': 'p.prj', 'grid': 'grid.i', 'input_path': ['01-zdir', '01-no-drag'], 'output_path': ['01-zdir', '01-no-drag']}
{'recursive': True, 'project': 'p.prj', 'grid': 'grid.i', 'input_path': ['01-zdir', '02-const-drag'], 'output_path': ['01-zdir', '02-const-drag']}
{'recursive': True, 'project': 'p.prj', 'grid': 'grid.i', 'input_path': ['01-zdir', '03-WenYu-drag'], 'output_path': ['01-zdir', '03-WenYu-drag']}

After the above for loop, I am doing

print(notes.values['project_suites'])

This gives me the following output:

[{'recursive': True, 'project': 'p.prj', 'grid': 'grid.i', 'input_path': ['01-zdir', '03-WenYu-drag'], 'output_path': ['01-zdir', '03-WenYu-drag']}, {'recursive': True, 'project': 'p.prj', 'grid': 'grid.i', 'input_path': ['01-zdir', '03-WenYu-drag'], 'output_path': ['01-zdir', '03-WenYu-drag']}, {'recursive': True, 'project': 'p.prj', 'grid': 'grid.i', 'input_path': ['01-zdir', '03-WenYu-drag'], 'output_path': ['01-zdir', '03-WenYu-drag']}]

Basically, the input_path and output_path seem to have only the last path_list for everyone. Any reason why this inconsistency is occurring?

SKPS
  • 5,433
  • 5
  • 29
  • 63
  • 1
    It seems like `notes.values['project_suites']` has a bunch of references to the same object, but since you didn't provide code that can reproducible the problem, all we can do is guess. (I noticed you linked to [Minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) in your bio here...) – Mark May 25 '22 at 01:33
  • @Mark : Apparently, the issue is to do with `list` duplicating which I did not show here. I did `deepcopy` and things seem to be working fine: https://stackoverflow.com/questions/40382487/copy-a-list-of-list-by-value-and-not-reference – SKPS May 25 '22 at 01:42

0 Answers0