Skip to content

Instantly share code, notes, and snippets.

@ashgti
Created October 19, 2011 23:40
Show Gist options
  • Select an option

  • Save ashgti/1300005 to your computer and use it in GitHub Desktop.

Select an option

Save ashgti/1300005 to your computer and use it in GitHub Desktop.
a = {'toggle1': {None: 51.5, 'z': False, 'foobar' : { 3 : 7 } } }
def flatten(aDict, path='', sep='/'):
for k, v in aDict.iteritems():
new_path = path
if k:
new_path += sep + str(k)
try:
for i in flatten(v, new_path):
yield i
except AttributeError:
yield (new_path, v)
print { k : v for k, v in flatten(a) }
print dict(flatten(a))
@mjcarroll
Copy link
Copy Markdown

def walkDict(self, aDict, path='', sep='/'):
    for k, v in aDict.iteritems():
        if k is not None:
            new_k = path + sep + str(k)
        else:
            new_k = path
        try:
            for i in self.walkDict(v, new_k):
                yield i
        except AttributeError:
            yield (new_k, v)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment