[Python] Limit and sort DynamoDB results with FilterExpression

Discussão em 'Python' iniciado por Stack, Julho 23, 2025.

  1. Stack

    Stack Membro Participativo

    How can I limit the number of results by using the filter expression? I can't use a key method and need to user a filter by attribute.

    In the below code it returns me like 500 records in response but I need to get only 10 latest.

    import json
    import boto3
    from boto3.dynamodb.conditions import Key, Attr

    dynamodb = boto3.resource('dynamodb')
    table = dynamodb.Table('users')

    def lambda_handler(event, context):
    response = table.scan(
    FilterExpression=Attr('age').eq('30') & Attr('created_at').lt('2020-01-01T00:00:00') & Attr('status').eq('enabled')
    )
    items = response['Items']
    #loadjson = json.stringify(event)
    #for data in event['data']:
    # print(data['key'])
    return items

    Continue reading...

Compartilhe esta Página