I'm trying to use recursion to traverse a list and append all its non-list elements and the elemnents of the sublists to a unique list.
def refr(a_list):
loo = []
for item in a_list:
if isinstance(item, list):
refr(item)
else:
loo.append(item)
return loo
print(refr([1, ['no', 'ok'], 'dang', 0]))
When I define loo as a local variable it only contains the elements of the input list that are not representing a list.
# output
[1, 'dang', 0]
But when defined it as globlal the output is correct
# output
[1, 'no', 'ok', 'dang', 0]
Why so?