Python Forum
Split dict of lists into smaller dicts of lists.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Split dict of lists into smaller dicts of lists.
#1
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...
Reply
#2
what have you tried? Post your code in python tags, any traceback - in error tags, ask specific questions.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
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'
Reply
#4
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  problem with print lists MarekGwozdz 4 676 Dec-15-2023, 09:13 AM
Last Post: Pedroski55
  python convert multiple files to multiple lists MCL169 6 1,534 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  Lists blake7 6 758 Oct-06-2023, 12:46 PM
Last Post: buran
  Trying to understand strings and lists of strings Konstantin23 2 757 Aug-06-2023, 11:42 AM
Last Post: deanhystad
  Why do the lists not match? Alexeyk2007 3 801 Jul-01-2023, 09:19 PM
Last Post: ICanIBB
  ''.join and start:stop:step notation for lists ringgeest11 2 2,423 Jun-24-2023, 06:09 AM
Last Post: ferdnyc
  Need help with sorting lists as a beginner Realist1c 1 737 Apr-25-2023, 04:32 AM
Last Post: deanhystad
  Pip lists the module but python does not find it Dato 2 1,269 Apr-13-2023, 06:40 AM
Last Post: Dato
  Generate lists of devices and partitions from /proc/partitions? DachshundDigital 1 765 Feb-28-2023, 10:55 PM
Last Post: deanhystad
  List all possibilities of a nested-list by flattened lists sparkt 1 915 Feb-23-2023, 02:21 PM
Last Post: sparkt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020