I want to write a function that extracts elements from deep nested tuples and lists, say I have something like this
l = (\'THIS\', [(\'THAT\', [\'a\', \'b\']),
This iterative function should do the trick alongside the .extend() list operator.
def func(lst):
new_lst = []
for i in lst:
if i != 'THAT' and i != 'THIS':
if type(i) == list or type(i) == tuple:
new_lst.extend(func(i))
else: new_lst.append(i)
return new_lst
l = ('THIS', [('THAT', ['a', 'b']), 'c', ('THAT', ['dk', 'e', 'f'])])
print(func(l))
['a', 'b', 'c', 'dk', 'e', 'f']