Skip to content

Bug: slice_dictionary yields duplicate chunks instead of slicing dictionary correctly #7252

@anafalcao

Description

@anafalcao

Expected Behaviour

slice_dictionary should yield consecutive chunks of dictionary items until the input dictionary is exhausted. Each chunk should contain up to chunk_size items without repetition.

Current Behaviour

The slice_dictionary helper currently does not slice dictionaries into chunks properly.
Because itertools.islice(data, chunk_size) always starts from the beginning of the dictionary, each iteration yields the same first chunk_size keys, resulting in duplicate chunks.

Code snippet

import itertools

def slice_dictionary(data: dict, chunk_size: int):
    for _ in range(0, len(data), chunk_size):
        yield {dict_key: data[dict_key] for dict_key in itertools.islice(data, chunk_size)}

data = {"k0": 0, "k1": 1, "k2": 2}
# actual   [{'k0': 0, 'k1': 1}, {'k0': 0, 'k1': 1}]
# expected [{'k0': 0, 'k1': 1}, {'k2': 2}]
print(list(slice_dictionary(data, 2)))

Possible Solution

No response

Steps to Reproduce

Code snippet from above.

Powertools for AWS Lambda (Python) version

latest

AWS Lambda function runtime

3.9

Packaging format used

PyPi

Debugging logs

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

Projects

Status

Coming soon

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions