python - Why does the None is getting appended in the list -


in code below,i want leaf elements dict.

group_children_map={'mould': ['yeast'], 'living organism': ['animal', 'plant', 'mould'], 'animal': ['lion', 'tiger', 'cat', 'dog'], 'plant': ['tulsi', 'hibiscus', 'aloe vera']} print group_children_map node='living organism' leaf_list=[] def find_leaf(node):     try_leaf=group_children_map.get(node)     if try_leaf none:         #print node         return node     else:         print try_leaf,"list"         l in try_leaf:             #print l             leaf_list.append(find_leaf(l))  find_leaf(node)  print leaf_list 

expected output:

['lion', 'tiger', 'cat', 'dog', 'tulsi', 'hibiscus', 'aloe vera', 'yeast'] 

actual result:

 ['lion', 'tiger', 'cat', 'dog', none, 'tulsi', 'hibiscus', 'aloe vera', none, 'yeast', none] 

why none getting appended in list...need :/

your find_leaf() function not explicitly return something. when function ends none returned instead.

the function return if try_leaf none true. when that's false, recursively call find_leaf(), after recursive calls don't return explicitly.

you explicitly test case:

for l in try_leaf:     leaf = find_leaf(l)     if leaf not none:         leaf_list.append(leaf) 

or instead move appending leaf_list other branch of code, instead of returning:

def find_leaf(node):     if node not in group_children:         leaf_list.append(node)     else:         l in group_children[node]:             find_leaf(l) 

Comments

Popular posts from this blog

How has firefox/gecko HTML+CSS rendering changed in version 38? -

javascript - Complex json ng-repeat -

jquery - Cloning of rows and columns from the old table into the new with colSpan and rowSpan -