Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Devon Lawler
Devon Lawler
4,995 Points

Trying to "shard" a dict in python ... missing something here!

Alright, so let's say for example I have a list of dictionaries that looks like the following:

[{u'body': u'This is going to be a much longer body as well here, I want this one to be longer too so that I can split it into a bunch of smaller chunks!!', u'fourth:': u'Last item'}, {u'body': u'This is going to be a much longer body, since this is the one that I want to match on!', u'third:': u'Yet another item'}, {u'body': u'Another body!!', u'second': u'Another item!', u'objectID': u'2713980'}, {u'body': u'this is the body!', u'objectID': u'2713970', u'first': u'first item'}, {u'name': u'Split Banana', u'objectID': u'2670950', u'description': u'And some more banana splits'}, {u'name': u'Banana Banana Split', u'objectID': u'2670940', u'description': u'Some other bananas'}, {u'name': u'Banana Split', u'objectID': u'2670930', u'description': u'Some bananas that are split'}]

I am basically trying to take any of the dicts that contain the key body, and if the length is greater than 30, split it into smaller components and then rebuild it as mirrored dictionaries, so one of the above bodies gets split into a list like:

[u'This is go', u'ing to be ', u'a much lon', u'ger body, ', u'since this', u' is the on', u'e that I w', u'ant to mat', u'ch on!']

I then want to recombine it into a new list, with all of the original keys as well, but with 'body' now split up among multiple dictionaries instead of just one. Where I'm getting stuck here, is my new list of dicts just seems to contain only the first item from the list. Using the above list as records_list I have the following code currently:

long_records = []
for i in range(len(records_list)):
    if "body" in records_list[i] and len(records_list[i]['body']) > 30:
        long_object_ids.append(records_list[i].pop('objectID', None))

full_body = []
split_body = []
rebuild_records = []
n = 10
for i in range(len(long_records)):
    full_body = long_records[i]['body']
    split_body = [full_body[f:f+n] for f in range(0, len(full_body), n)]
    long_records[i].pop('body', None)
    for x in range(len(split_body)):
        rebuild_records[x]['body'] = split_body.pop()

Hopefully this in some way makes sense, happy to clarify any confusing parts! :)