aws-powertools/powertools-lambda-python

Bug: slice_dictionary yields duplicate chunks instead of slicing dictionary correctly

Closed this issue · 1 comments

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

Warning

This issue is now closed. Please be mindful that future comments are hard for our team to see.
If you need more assistance, please either reopen the issue, or open a new issue referencing this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.