The title of the question is too broad, and the author needs more specific information. In my case, I needed to extract all the elements from a nested list, as in the example below :
Example:
input -> [1,2,[3,4]] output -> [1,2,3,4]
The code below gives me the result, but I would like to know if anyone can create a simpler answer:
def get_elements_from_nested_list(l, new_l): if l is not None: e = l[0] if isinstance(e, list): get_elements_from_nested_list(e, new_l) else: new_l.append(e) if len(l) > 1: return get_elements_from_nested_list(l[1:], new_l) else: return new_l
Method call
l = [1,2,[3,4]] new_l = [] get_elements_from_nested_list(l, new_l)
source share