Single-item operations by key are the bread and butter of every DynamoDB application.
All single-item operations take the full primary key (partition key, plus sort key if the table has one).
put_item(Item=...)writes a whole item. If an item with that key exists, it is replaced entirely: attributes you did not include are gone.get_item(Key=...)reads one item. The response has anItemfield only if it exists, so useresp.get("Item"). Reads are eventually consistent by default; passConsistentRead=Truefor a strongly consistent read.update_item(Key=..., UpdateExpression=...)changes some attributes in place and creates the item if it does not exist.ReturnValues="ALL_NEW"returns the updated item, saving a separate read.delete_item(Key=...)removes an item; deleting a missing item is not an error.
Each write is atomic for that item, and all four accept a ConditionExpression (covered later) to make the operation happen only if the item is in an expected state.
Remember the asymmetry: put replaces, update merges. Use update_item for counters, status changes and partial edits, and put_item when you own the whole item.
ProjectionExpression on get_item returns only the attributes you list, which saves bandwidth but not read capacity: you pay for the full item size.
Example
import boto3
table = boto3.resource("dynamodb").Table("Orders")
key = {"customer_id": "c1", "order_id": "2025-03-01#A17"}
table.put_item(Item={**key, "total": 35, "status": "PENDING"})
resp = table.update_item(
Key=key,
UpdateExpression="SET #s = :s",
ExpressionAttributeNames={"#s": "status"}, # status is a reserved word
ExpressionAttributeValues={":s": "SHIPPED"},
ReturnValues="ALL_NEW",
)
print(resp["Attributes"]["status"]) # SHIPPED
print(table.get_item(Key=key)["Item"]["total"]) # 35
table.delete_item(Key=key)
print(table.get_item(Key=key).get("Item")) # NoneRemember
- Every single-item call needs the full primary key
- put_item replaces the whole item; update_item modifies attributes and upserts
- get_item returns no Item key when the item does not exist
- Use ReturnValues to get the new or old item back from a write
Watch out for
- Using put_item to change one attribute and accidentally wiping all the others.