I am trying to change the structure of an object inside of an array that I am looping though with a for loop. Nothing seems to happen to the object unless I append it to a new array.
I just can't help but think there is a better way to do this...
I have replaced all the real data and naming with placeholder data just for structure.
Essentially though I just want to restructure obj in the original_array rather than appending to a new one.
rebuilt_array = []
for obj in original_array:
rebuilt_obj = {
"inner_obj_1": {
"item_1": obj["old_item_1"],
"item_2": obj["old_item_2"],
"item_3": obj["old_item_3"],
},
"another_item": obj["old_another_item"],
"inner_obj_2": {
"item_1": obj["old_item_1"],
"item_2": obj["old_item_2"],
"item_3": obj["old_item_3"],
},
}
rebuilt_array.append(rebuilt_obj)
obj? Is it adictor some custom object?dictobjis a local variable, so even if you modify it, it won't be reflected in the original list. In order to modify objects inoriginal_array, you will have to use something likefor i, obj in enumerate(original_array)