Radio Controlled
Radio Controlled

Reputation: 950

Python: Getting all strings inside dictionary structure

WARNING: This is still Python 2.7!

For some reason the following function has extremely slow runtime:

def get_content(d,content):
    if isinstance(d,unicode):
        content += d
        content += u'\n'
    elif isinstance(d,dict):
        for key in d:
            content += get_content(d[key],content)
    elif isinstance(d,list):
        for el in d:
            content += get_content(el,content)
    return content

content = get_content(D,u'')

That is although D is quite small. Nothing crazy going on there size-wise.

Do you see what the problem is or what would be a better way to solve the task?

EDIT: I changed the code to ...

def get_content(d,content):
    if isinstance(d,unicode):
        content += [d]
    elif isinstance(d,dict):
        for key in d:
            content += get_content(d[key],content)
    elif isinstance(d,list):
        for el in d:
            content += get_content(el,content)
    return content

content = get_content(D,[])

... and it still has the same problem.

Upvotes: 0

Views: 50

Answers (1)

ScootCork
ScootCork

Reputation: 3686

The problem is that you are reappending the whole content at each recursion. To solve pass an empty list to each get_content call instead.

def get_content(d,content):
    if isinstance(d,unicode):
        content += [d]
    elif isinstance(d,dict):
        for key in d:
            content += get_content(d[key],[])
    elif isinstance(d,list):
        for el in d:
            content += get_content(el,[])
    return content

content = get_content(D,[])

Upvotes: 2

Related Questions