1

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)
5
  • 1
    Looks reasonable to me. If you don't plan on adding obj_3 or item_4, then this is probably clearer than generic code would be. Commented Dec 21, 2022 at 5:33
  • 1
    What is the type of the obj? Is it a dict or some custom object? Commented Dec 21, 2022 at 5:33
  • 1
    I would have personally used a comprehension, but for loop + append is OK too. Commented Dec 21, 2022 at 5:34
  • 1
    @Jay it is a dict Commented Dec 21, 2022 at 5:35
  • obj is a local variable, so even if you modify it, it won't be reflected in the original list. In order to modify objects in original_array, you will have to use something like for i, obj in enumerate(original_array) Commented Dec 21, 2022 at 5:44

1 Answer 1

2

When you do for obj in original_array, obj is a local variable and a copy of the original object in the list, so modifying it won't reflect it in the list, here's a way you can do the same thing without creating a new list and updating the list in-place -

original_array = [{"old_item_1": 1, "old_item_2": 2, "old_item_3": 3, "old_another_item": -1}]

for i, obj in enumerate(original_array):
    new_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"],
        },
    }
    original_array[i] = new_obj

print(original_array)

Output:

[{'inner_obj_1': {'item_1': 1, 'item_2': 2, 'item_3': 3}, 'another_item': -1, 'inner_obj_2': {'item_1': 1, 'item_2': 2, 'item_3': 3}}]

You can also further modify it so that we don't need the new_obj creation and you can update the older object in place directly by updating original_array[i] dict as you would a normal dict. e.g. original_array[i]['another_item'] = original_array[i].pop("old_another_item") which would rename old_another_item key to another_item in your original object, but this might become dirtier compared to above approach.

Here's the corresponding code for it -

for i in range(len(original_array)):
    original_array[i] = {
        "inner_obj_1": {
            "item_1": original_array[i]["old_item_1"],
            "item_2": original_array[i]["old_item_2"],
            "item_3": original_array[i]["old_item_3"],
        },
        "another_item": original_array[i].pop("old_another_item"),
        "inner_obj_2": {
            "item_1": original_array[i].pop("old_item_1"),
            "item_2": original_array[i].pop("old_item_2"),
            "item_3": original_array[i].pop("old_item_3"),
        }
    }

Note the use of pop() to rename dict keys. If you want to keep original keys in your dict as well, then you can skip using pop().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.