Python Forum
Split dict of lists into smaller dicts of lists. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Split dict of lists into smaller dicts of lists. (/thread-29755.html)



Split dict of lists into smaller dicts of lists. - pcs3rd - Sep-18-2020

I am trying to upload a bunch of data to squarespace, which requires that no more than 50 items are sent at a time.
Currently, squarespace uses the following format for editing item quantities:


Output:
{'setFiniteOperations': [{'variantId': '759ecdae-f30b-4adb-9673-5645cecd1516', 'quantity': 3}, {'variantId': 'b3a04ad1-9c63-4822-ab0a-9332d57408e4', 'quantity': 2}, {'variantId': '25c01c23-dcd0-4812-b1f4-25269bca04f9', 'quantity': 2}, {'variantId': '9532d2c0-6639-4d54-9520-05c2febbb3c6', 'quantity': 7}, {'variantId': '8db787ad-b087-4377-a452-5aa749cf2bd1', 'quantity': 6}, {'variantId': '369035fc-ad0b-40c4-b232-c8ebd8f2c973', 'quantity': 2}, {'variantId': '90b29266-326c-42db-a3e7-8d41c377f8da', 'quantity': 4}, {'variantId': 'e4eeee7b-e67f-4728-a2b7-a5230291cfc0', 'quantity': 6}, {'variantId': '99401853-b260-44b9-b4f4-5cec21ad6ee7', 'quantity': 1}, {'variantId': 'ab212ead-0f51-4c53-86b3-de5850baa68c', 'quantity': 5}, {'variantId': '8bd4c0d7-4cea-4915-b292-8ffedbcebb69', 'quantity': 6}, {'variantId': '83e0852a-1500-4fb1-b2b1-9482f1b3d9ff', 'quantity': 6}, {'variantId': '7a28eb8f-936e-4cf2-b43b-63eb2498eedc', 'quantity': 1}, {'variantId': '2d3b6f1b-f2e2-42fe-beae-d6f65b22a115', 'quantity': 0}, {'variantId': '1500aa96-9fc7-4923-935e-49cfceea4b62', 'quantity': 17}, {'variantId': '2b0a1eeb-ac1f-42cd-895b-d4aafba5d2f5', 'quantity': 8}, {'variantId': 'bfdd5eb5-521f-443c-986f-73b6b9c5de9f', 'quantity': 8}, {'variantId': 'da262d8f-0ac5-4d53-b994-2a98be72ed19',[...]}
How should I go about reducing this into chunks of 50 so it looks like this?:
Output:
split1 = {'setFiniteOperations':[50...]} split2 = {'setFiniteOperations':[50...]} and so on...



RE: Split dict of lists into smaller dicts of lists. - buran - Sep-18-2020

what have you tried? Post your code in python tags, any traceback - in error tags, ask specific questions.


RE: Split dict of lists into smaller dicts of lists. - pcs3rd - Sep-18-2020

I have tried this:
# Initialize dictionary
test_dict = {'setFiniteOperations': [{'variantId': '759ecdae-f30b-4adb-9673-5645cecd1516', 'quantity': 9}, {'variantId': '759ecdae-f30b-4adb-9673-5645cecd1516', 'quantity': 10}]}
  
# printing original dictionary
print("The original dictionary : " +  str(test_dict))
  
# Using items() + len() + list slicing
# Split dictionary by half
res1 = dict(list(test_dict['setFiniteOperations'].items())[len(test_dict['setFiniteOperations'])//2:])
res2 = dict(list(test_dict['setFiniteOperations'].items())[:len(test_dict['setFiniteOperations'])//2])
  
# printing result
print("The first half of dictionary : " + str(res1))
print("The second half of dictionary : " + str(res2))
and this is the traceback:
Output:
The original dictionary : {'setFiniteOperations': [{'variantId': '759ecdae-f30b-4adb-9673-5645cecd1516', 'quantity': 9}, {'variantId': '759ecdae-f30b-4adb-9673-5645cecd1516', 'quantity': 10}]} Traceback (most recent call last): File "post.py", line 13, in <module> res1 = dict(list(test_dict['setFiniteOperations'].items())[len(test_dict['setFiniteOperations'])//2:]) AttributeError: 'list' object has no attribute 'items'



RE: Split dict of lists into smaller dicts of lists. - ibreeden - Sep-19-2020

The message is quite clear. test_dict is a dictionary. So a valid operation is: test_dict.items(). But test_dict['setFiniteOperations'] is a list. So you cannot apply items() on that.