How to specify index of specific elements in every sublist of nested list?
If a sub-list
contains 'b' or 'd'
that element must be in the first index [0]
:
x = [
['a', [['b', 'c', [['e', 'd']]]]],
['a', [['b', [['e', 'd']], 'c']]],
[[['b', 'c', [['e', 'd']]]], 'a'],
['a', [[[['d', 'e']], 'c', 'b']]],
['a', [['b', [['d', 'e']], 'c']]]
]
def limitation(nested):
for index, subelement in enumerate(nested):
if isinstance(subelement, list):
if not limitation(subelement):
return False
else:
if subelement in ['d', 'b'] and not index:
return False
return True
for element in x:
if limitation(element):
print(element) # ['a', [['b', [['d', 'e']], 'c']]]