Boto3
AWS SDK
Python
updating values
cloud computing

Boto3 updating multiple values

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

When people ask about updating multiple values with Boto3, they are often working with DynamoDB and want to change several attributes in one request. The usual solution is update_item with a single UpdateExpression that sets every target attribute together.

Update Multiple Attributes In One update_item Call

In DynamoDB, a single update_item call can modify more than one field on the same item. The normal pattern is to build one SET expression with several assignments.

python
1import boto3
2from decimal import Decimal
3
4dynamodb = boto3.resource("dynamodb")
5table = dynamodb.Table("Products")
6
7response = table.update_item(
8    Key={"product_id": "A-100"},
9    UpdateExpression="SET price = :price, stock = :stock, status = :status",
10    ExpressionAttributeValues={
11        ":price": Decimal("19.99"),
12        ":stock": 42,
13        ":status": "active",
14    },
15    ReturnValues="ALL_NEW",
16)
17
18print(response["Attributes"])

This updates price, stock, and status in one request. ReturnValues="ALL_NEW" asks DynamoDB to return the updated item attributes so you can confirm the result immediately.

Use Expression Attribute Names For Reserved Words

Sometimes an attribute name conflicts with a DynamoDB reserved word, or it contains characters that are awkward inside an expression. In those cases, alias the attribute names.

python
1import boto3
2
3dynamodb = boto3.resource("dynamodb")
4table = dynamodb.Table("Orders")
5
6response = table.update_item(
7    Key={"order_id": "ORD-1"},
8    UpdateExpression="SET #state = :state, #updated_at = :updated_at",
9    ExpressionAttributeNames={
10        "#state": "state",
11        "#updated_at": "updated_at",
12    },
13    ExpressionAttributeValues={
14        ":state": "shipped",
15        ":updated_at": "2026-03-11T10:30:00Z",
16    },
17    ReturnValues="UPDATED_NEW",
18)
19
20print(response["Attributes"])

This makes the update expression easier to maintain and prevents odd failures caused by reserved keywords.

Combine SET, REMOVE, And Arithmetic Updates

update_item is not limited to plain replacements. You can combine different update operations in one statement.

python
1import boto3
2
3dynamodb = boto3.resource("dynamodb")
4table = dynamodb.Table("Users")
5
6table.update_item(
7    Key={"user_id": "u-1"},
8    UpdateExpression="SET login_count = login_count + :one, last_login = :ts REMOVE temporary_code",
9    ExpressionAttributeValues={
10        ":one": 1,
11        ":ts": "2026-03-11T12:00:00Z",
12    },
13)

This increments login_count, replaces last_login, and removes temporary_code in a single request for the same item. That is often cleaner and safer than issuing several separate updates back to back.

Add Conditions When The Update Must Be Safe

Many real updates should happen only if the item is already in a specific state. DynamoDB supports this with ConditionExpression.

python
1import boto3
2from botocore.exceptions import ClientError
3
4dynamodb = boto3.resource("dynamodb")
5table = dynamodb.Table("Orders")
6
7try:
8    table.update_item(
9        Key={"order_id": "ORD-55"},
10        UpdateExpression="SET #state = :new_state, shipped_at = :ts",
11        ExpressionAttributeNames={"#state": "state"},
12        ExpressionAttributeValues={
13            ":new_state": "shipped",
14            ":ts": "2026-03-11T12:15:00Z",
15            ":expected_state": "paid",
16        },
17        ConditionExpression="#state = :expected_state",
18    )
19except ClientError as exc:
20    if exc.response["Error"]["Code"] == "ConditionalCheckFailedException":
21        print("Order was not in the expected state")
22    else:
23        raise

This is important when several workers or requests might try to update the same item concurrently.

Know What "Multiple Values" Does And Does Not Mean

A single update_item call can update many attributes on one item. It does not mean you are updating many different items at once. If you need to touch several items atomically, you are in transaction territory instead of ordinary update_item territory.

That distinction matters because developers sometimes expect one update expression to act like a batch update across many partition keys. DynamoDB does not work that way.

Common Pitfalls

  • Updating multiple attributes on one item but forgetting that DynamoDB numbers should often use Decimal in Python.
  • Writing attribute names directly in the expression when a reserved word requires an alias.
  • Assuming one update_item call can update several different items.
  • Splitting logically related changes into many requests when one update expression would be clearer.
  • Forgetting ConditionExpression when correctness depends on the item's existing state.

Summary

  • Use one update_item call with a multi-part UpdateExpression to change several attributes on one DynamoDB item.
  • Alias attribute names with ExpressionAttributeNames when necessary.
  • Combine SET, arithmetic changes, and REMOVE operations when that matches the update logic.
  • Add ConditionExpression when correctness depends on the item's existing state.

Course illustration
Course illustration

All Rights Reserved.