I want to convert a simple dict like so: d = {0:0, 1:1, 2:2, 3:3}
to a list of dict with the same amount of elements in each of them (or so) as this one: [{0:0, 2:2}, {1:1, 3:3}]
. I've tried with simple indexing like I use with lists but it throws me a TypeError: unhashable type: 'slice'
. Here what i have right now:
def dico_chunks(dico, n):
if len(dico) < n:
n = len(dico)
return [dico[i::n] for i in range(n)]
Keep in mind that i don't need the list to be order in anyway. I just need to have my main dict splited in a list of n
sub-dicts.