Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python

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_records.append(records_list[i])
        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.append(long_records[i])
        rebuild_records[x]['body'] = split_body.pop()

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