DynamoDB GetItem in Python (boto3)
get_item reads a single item by its full primary key. The boto3 resource API (Table.get_item) takes and returns native Python types, so you pass plain strings and numbers — no {"S": ...} descriptors.
Code
import boto3
dynamodb = boto3.resource("dynamodb", region_name="us-east-1")
table = dynamodb.Table("Music")
def get_song():
response = table.get_item(
Key={
"Artist": "Arturo Sandoval",
"SongTitle": "Cubano Chant",
},
# Optional: return only the attributes you need
ProjectionExpression="Artist, SongTitle, AlbumTitle, #yr",
ExpressionAttributeNames={"#yr": "Year"},
)
item = response.get("Item")
if item is None:
print("Item not found")
return None
print(item)
# {'Artist': 'Arturo Sandoval', 'SongTitle': 'Cubano Chant', 'Year': Decimal('1981'), ...}
return item
if __name__ == "__main__":
get_song()Numeric attributes come back as decimal.Decimal, not float. Convert explicitly (int(item["Year"])) when you need a native number.
Explanation
dynamodb.Table("Music")— the resource-level handle; it auto-marshals native types both ways.Key— must include every primary-key attribute: partition key only for a simple key, partition and sort key for a composite key. A partial key raisesValidationException.ProjectionExpression— optional; limits which attributes return.Yearis a reserved word, so it's aliased viaExpressionAttributeNames(#yr).response.get("Item")— theItemkey is absent when nothing matches; use.get()rather thanresponse["Item"].- Reads are eventually consistent by default. Pass
ConsistentRead=Truefor a strongly-consistent read (2× the RCU, base table only).
Do it visually
Assembling the projection or a conditional read by hand? Use the free DynamoDB Expression Builder to build the expression and copy the exact ProjectionExpression / ExpressionAttributeNames.
To browse tables and run GetItem in a GUI — key form, results grid, copy the generated boto3 code — download DynoTable.
Related guides
- Query vs. Scan — when a single
get_itembeats aquery. - DynamoDB data types — why numbers arrive as
Decimal.