0

Well guys, I created a dict, and divided it using the function:

listOfDicts = [{k:v for k,v in dictionary.items() if k%10==i} for i in range(10)]

From that, I got 10 sublists:

listOfDicts[0 a 9]
listOfDict[0]: {0: 0, 10: 5, 20: 10, 30: 15, 40: 20, 50: 25, 60: 30, 70: 35, 80: 40, 90: 45}

But what if I want to divide the sublists into equal sizes (in the case size = 3) and add in a single dict:

listOfDict[0]: {{0: 0, 10: 5, 20: 10}, {30: 15, 40: 20, 50: 25}, {60: 30, 70: 35, 80: 40}, {90: 45}}
jusintique
  • 13
  • 4

1 Answers1

0

Try an approach like this:

chunks = [data[x:x+100] for x in range(0, len(data), 100)]

Reference: Split a python list into other "sublists" i.e smaller lists

For dictionaries, you can use this approach:

d = {'key1': 1, 'key2': 2, 'key3': 3, 'key4': 4, 'key5': 5}
d1 = dict(d.items()[len(d)/2:])
d2 = dict(d.items()[:len(d)/2])

Note that you need to adapt to your reality first.

Reference: Split a dictionary in half?

You can convert your dictionary to a list:

chunks = [list(data.items())[x:x+100] for x in range(0, len(data), 100)]

And chunks will looks like this:

[[('a', 1), ('b', 2)], [('c', 3), ('d', 4)]]
briba
  • 2,857
  • 2
  • 31
  • 59