Hands-On: Read & Write with Python
Put, get, and query items with boto3, and see why Query beats Scan.
1 min read
boto3's resource interface lets you use plain Python dicts. These examples use a table keyed by userId
(partition) + createdAt (sort) so we can show off queries.
Try it
dynamo_demo.py
import boto3
from boto3.dynamodb.conditions import Key
table = boto3.resource("dynamodb").Table("notesnap-notes")
# Write: put_item creates or fully replaces an item
table.put_item(Item={
"userId": "sam",
"createdAt": "2026-11-18T14:30Z",
"summary": "Notes from team standup",
"wordCount": 212,
})
# Read one item by its full primary key: the fastest operation there is
item = table.get_item(Key={"userId": "sam", "createdAt": "2026-11-18T14:30Z"}).get("Item")
print("One item:", item["summary"])
# Query: all of Sam's notes from November, newest first
resp = table.query(
KeyConditionExpression=Key("userId").eq("sam") & Key("createdAt").begins_with("2026-11"),
ScanIndexForward=False, # descending by sort key
)
for note in resp["Items"]:
print(note["createdAt"], "-", note["summary"])Query vs. Scan
- Query jumps straight to one partition key and reads only matching items. It stays fast at any table size.
- Scan reads every item in the table, then filters. It gets slower and pricier as the table grows.
Watch out
If you find yourself writing scan() with a filter in production code, it usually means you need a different
key design or a GSI.
Note
Numbers come back from boto3 as Python Decimal, not int or float. Convert them before calling
json.dumps.
Key takeaways
- get_item needs the full primary key and is the fastest read.
- query reads one partition (optionally narrowed by sort key) and scales well.
- scan reads the whole table, so avoid it on hot paths.
Check your understanding
Pick an answer to see the explanation.
1. Your table has 50 million items. Which call stays fast as the table grows?