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.
#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


Messages In This Thread
RE: Split dict of lists into smaller dicts of lists. - by pcs3rd - Sep-18-2020, 05:00 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Appending lists into lists from function is not working for me BoredBannana 2 1,146 Oct-25-2024, 05:18 PM
Last Post: deanhystad
  Nested Lists & Dictionaries Hudjefa 5 1,317 Sep-23-2024, 08:20 PM
Last Post: DeaD_EyE
Sad The issue with compare 2D lists przonak007 18 3,301 Jul-17-2024, 07:31 AM
Last Post: Pedroski55
  remove duplicates from dicts with list values wardancer84 27 5,574 May-27-2024, 04:54 PM
Last Post: wardancer84
  Compare lists w_i_k_i_d 6 1,637 May-23-2024, 07:23 PM
Last Post: deanhystad
Question Using Lists as Dictionary Values bfallert 8 2,203 Apr-21-2024, 06:55 AM
Last Post: Pedroski55
  problem with print lists MarekGwozdz 4 1,625 Dec-15-2023, 09:13 AM
Last Post: Pedroski55
  python convert multiple files to multiple lists MCL169 6 3,242 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  Lists blake7 6 2,169 Oct-06-2023, 12:46 PM
Last Post: buran
  Trying to understand strings and lists of strings Konstantin23 2 1,749 Aug-06-2023, 11:42 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

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