I have a browser view method which returns an OrderedDict containing at most 18 brains, originated by something similar to this:
mydict = OrderedDict()
for b in brains:
video_obj = b.getObject()
mydict[b] = {'video_id': video_obj.url,
'thumb': video_obj.tag(scale='thumb')}
return mydict
I can easily loop through them generating a flat UL > LI
list in ZPT:
<ul>
<li tal:repeat="brain mydict">
<a tal:attributes="href python:mydict[brain]['video_id']">
<img tal:replace="mydict[brain]['thumb']" />
</a>
</li>
</ul>
But what I want is a batch-like structure:
<ul>
<li>
<a>Item 1</a>
<a>Item 2</a>
<a>Item 3</a>
</li>
...
<li>
<a>Item 16</a>
<a>Item 17</a>
<a>Item 18</a>
</li>
</ul>
Eg: I'd like to transform this {1:2, 2:4, 3:6, 4:8, 5:10, 6:12, 7:14, 8:16, 9:18}
into this [{1:2, 2:4, 3:6}, {4:8, 5:10, 6:12}, {7:14, 8:16, 9:18}]
.
I prefer not to change the browser view since other parts of the template expect mydict's keys to be brains.
BTW if I had a list instead of a dict I could regroup its elements with:
zip(*[mylist[i::3] for i in range(3)])
But that's not possible for dicts and (strangely imo) not for OrderedDicts either.
I think this is what you you really want:
from collections import OrderedDict
mydict = OrderedDict()
for i in xrange(18, 0, -1):
mydict['d{0}'.format(i)] = 'item {0}'.format(i)
regroupped_dict = [OrderedDict(mydict.items()[i: i + 2])
for i in xrange(0, len(mydict), 3)]
print regroupped_dict
# Result: [OrderedDict([('d18', 'item 18'), ('d17', 'item 17')]), OrderedDict([('d15', 'item 15'), ('d14', 'item 14')]), OrderedDict([('d12', 'item 12'), ('d11', 'item 11')]), OrderedDict([('d9', 'item 9'), ('d8', 'item 8')]), OrderedDict([('d6', 'item 6'), ('d5', 'item 5')]), OrderedDict([('d3', 'item 3'), ('d2', 'item 2')])]