Updating python list with new values (appending)

Viewed 23

I am running a code to update values in AWS dynamoDB. Logic is I will be reading in already existing values from DynamoDB and update it with new values. Entries in DynamoDB is a list of dictionaries (sample data below).

I am able to update with the code. But if there is a new entry which was not there previously in dynamoDb, i am appending it.

Appending part is not working as intended, I am getting duplicate values.

New values: enter image description here

Previous entried in dynamoDB enter image description here

enter image description here

'inventory.tz_distributor_aud' and 'dispensary.crm_account' should have got appended, instead inventory.tz_distributor_aud got appended twice.

What is the issue with the code?

print('previous_dynamo_entry:',previous_item['tables'])

for i in range(0, len(previous_item['tables'])):
                if type(previous_item['tables'][i]) == list:
                    previous_item['tables'][i] = previous_item['tables'][i][0] 
                
                table_data = [data for data in item['tables']
                                    if data['table'] == previous_item['tables'][i]['table']]
                
                if len(table_data) > 0:
                    previous_item['tables'][i] = table_data[0]
                    
print('\nupdated data',previous_item['tables'])   

new_tables=list(set([item['tables'][j]['table'] for j in range(0, len(item['tables']))])-set([previous_item['tables'][k]['table'] for k in range(0, len(previous_item['tables']))]))
print('\nnew_tables_added:',new_tables)

for i in range(0,len(new_tables)):
    previous_item['tables'].append(item['tables']['table'==[new_tables[i]]])
    
print('\nnew_dynamo_entry:',previous_item['tables'])

This is the part which is not working

for i in range(0,len(new_tables)):
    previous_item['tables'].append(item['tables']['table'==[new_tables[i]]])
0 Answers
Related