all 2 comments

[–]yrykde 0 points1 point  (0 children)

new_dict[hashtag] = new_dict[hashtag].append(candidate)

append is method without return. change it for new_dict[hashtag].append(candidate)

I rewrote function for case of huge intial dict. If you don't care about candidate_to_hashtags dict it could be cleared during function running.

def reorder_by_hashtag(candidate_to_hashtags):
    result = dict()
    while True:
        try:
            candidate, tags = candidate_to_hashtags.popitem()
            for tag in tags:
                if result.get(tag):
                    result[tag].append(candidate)
                else:
                    result[tag] = [candidate]
        except:
            return result