Question: In python how do you iterate and append a dict of each object from an array into a new array (for serializing the data to return json in a GET request)? The existing iteration is returning duplicates of the last object in the array as shown in current results below and not printing the intended results below.

**Note:**

*I am using SQLAlchemy's .get.all() to get the initial messages and they serve as not JSON serializable, I am trying to stay away from libraries such as Marshmallow. Flask `.jsonify(response)` and `json.dumps(response)` throw errors.*

```
message = [
        {
            'id': 1,
            'message': the first message,
        },
        {
            'id': 2,
            'message': the second message
        }
    ]

response = []
row_data = {}

for message in messages:
    for k, v in message.items():
        row_data[k] = str(v)
    response.append(row_data)
print(response)

```

current response:

```
[
    {
        'id': '2',
        'message': 'the second message',
    },
    {
        'id': '2',
        'message': 'the second message'
    }
]

```

intended response:

```
[
    {
        'id': '1',
        'message': 'the first message',
    },
    {
        'id': '2',
        'message': 'the second message'
    }
]

```

Answer: 
IIUC, you want to read the input from `message` and get an output with keys and values in strings.

```
response = [{k:str(v) for k,v in msg.items()} for msg in message]

```

This will give you the following output:

```
[{'id': '1', 'message': 'the first message'}, {'id': '2', 'message': 'the second message'}]

```

Looks like this is what you are trying to do with the two for loops and append statement.
You will understand what went wrong if you `print(row_data)` after your loop:

```
{
    'id': 2,
    'message': 'the second message'
}

```

There are only ever two keys in `row_data`: `'id'` and `'message'`, which you overwrite in your loop. When you append `row_data` to your `response` `list` you add only the reference to `row_data`, which, as you see above, only contains reference two the second message.

There is an easy workaround however. You don't need the inner loop:

```
messages = [
    {
        'id': 1,
        'message': 'the first message',
    },
    {
        'id': 2,
        'message': 'the second message'
    }
]

response = []
row_data = {}

for message in messages:
    response.append(message)
print(response)

```

Although, as pointed out already, you don't need loops at all in this case and should be able to dump the `messages` as is.