-
Notifications
You must be signed in to change notification settings - Fork 450
Closed
Copy link
Labels
bugSomething isn't workingSomething isn't working
Description
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 workingSomething isn't working
Type
Projects
Status
Coming soon