Skip to content

Instantly share code, notes, and snippets.

@gulnara
Created July 28, 2013 20:32
Show Gist options
  • Save gulnara/6100119 to your computer and use it in GitHub Desktop.
Save gulnara/6100119 to your computer and use it in GitHub Desktop.
nested dictionary solution
import pprint
def nested_tree(edges):
nested = {}
for i in edges:
key = i[0]
value = {}
temp = i[1]
if nested.get(key):
nested[key][temp]={}
else:
nested[key] = value
value[temp]= {}
pprint.pprint(nested)
if __name__ == '__main__':
edges = [
['a', 'b'],
['a', 'f'],
['a', 'j'],
['b', 'c'],
['b', 'd'],
['d', 'e'],
['f', 'g'],
['g', 'h'],
['g', 'i'],
]
nested_tree(edges)
# nested = {
# 'a': {
# 'b': {
# 'c': { },
# 'd': {
# 'e': { }
# }
# },
# 'f': {
# 'g': {
# 'h': { },
# 'i': { },
# },
# },
# 'j': { },
# }
# }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment