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: How should I go about reducing this into chunks of 50 so it looks like this?:
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:
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. |