'How to delete items from dynamodb list
- listofvalues to remove = [1,2,3]
- myList from dynamodb = [1,2,3,4,5,6,7]
- Do i need to loop every single item find the index and remove
- Is there any better approach
- Gone through link Add or remove an entry from a List type attribute in a DynamoDB table item
import boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('myowntable')
response = []
response_table = table.get_item(Key={'id': '19'})
if 'Item' in response_table and response_table['Item']:
response.append(response_table['Item'])
listofvalues = [1,2,3]
for i in listofvalues:
id_index_delete = str(response[0]['myList'].index(i))
query = "REMOVE myList[" + id_index_delete + "]"
table.update_item(
Key={
'id': '19'
},
UpdateExpression=query
)
Solution 1:[1]
If you turn your list into a map, you can then delete items based on the map key and do all deletes in one operation.
Old: [1,2,3]
New: {“1”:1,”2”:2,”3”:3}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ross Williams |
