writer.writerow() doesn't write to the correct column
You want to collect related rows together into a single list to write on a single row, something like:
missing = [] # collection for missing_responsesinstalled = [] # collection for installed_responses# Find instances IDs from the missing table in the master table to pull tag metadata for instances in missing_response['Items']: instance_missing = instances['missing_instances']['S'] #print("Missing:" + instance_missing) query_missing = all_instances_table.query(KeyConditionExpression=Key('ID').eq(instance_missing)) for item_missing in query_missing['Items']: missing_id = item_missing['ID'] missing_account = item_missing['Account'] missing_tags = item_missing['Tags'] missing_env = item_missing['Environment'] # Update first half of row with missing list missing.append(missing_id, missing_account, missing_tags, missing_env)# Find instances IDs from the installed table in the master table to pull tag metadatafor instances in installed_response['Items']: instance_installed = instances['installed_instances']['S'] #print("Installed:" + instance_installed) query_installed = all_instances_table.query(KeyConditionExpression=Key('ID').eq(instance_installed)) for item_installed in query_installed['Items']: installed_id = item_installed['ID'] print(installed_id) installed_account = item_installed['Account'] installed_tags = item_installed['Tags'] installed_env = item_installed['Environment'] # update second half of row by updating installed list installed.append(installed_id, installed_account, installed_tags, installed_env)# combine your two lists outside a loopthis_row = []i = 0;for m in missing: # iterate through the first half to concatenate with the second half this_row.append( m + installed[i] ) i = i +1# adding an empty column after the write operation, manually, is optional# Write the data to the CSV file writer.writerow(this_row)
This will work if your installed and missing tables operate on a relatable field - like a timestamp or an account ID, something that you can ensure keeps the rows being concatenated in the same order. A data sample would be useful to really answer the question.