I have a simple DynamoDB table structure:
{
id: '123_456',
number_list: ['a', 'c']
}
And I would like to add 'b'
in the middle (or at some specific index) of this array.
What I know is that we can use list_append()
to add it at the end such as:
table.update_item(
Key={'id': '123_456'},
UpdateExpression="SET number_list = list_append(number_list, :nl)"
ExpressionAttributeValues={ ':nl': ['b'] }
)
This will result in ['a', 'c', 'b']
but I want ['a', 'b', 'c']
.
Is there a way to achieve this or should I get the stored list, add at index in python and store it again with:
SET list_number = :nln
{ ':nln': ['a', 'b', 'c'] }
I know we can delete at index with REMOVE list_number[1]
which will result in ['a']
. I would like to find something similar.
Many thanks in advance.
You can construct new list from older one - if you know exactly where change should happen. Here is an example - search for "Appending to a list and updating a specific value at the same time". From that I would try with:
UpdateExpression=f"SET #nl[1] = :repl",
ExpressionAttributeNames={
"#nl": "number_list",
},
ExpressionAttributeValues={
":repl": "b"
},
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.