Вы можете использовать следующую рекурсивную функцию:
def get_parent_list(the_elem, the_list):
if (the_elem == the_list):
return (True, the_elem)
elif the_elem in the_list:
return (True, the_list)
else:
for e in the_list:
if (type(e) is list):
(is_found, the_parent) = get_parent_list(the_elem, e)
if (is_found):
return (True, the_parent)
return (False, None)
Тестирование:
my_list=[[[['a','b'],['c','d'],['e','f']],[['1','2'],['3','4'],['5','6']]],
[[['a','b'],['c','d'],['e','f']],[['1','2'],['3','4'],['5','6']]]]
Контрольный пример 1:
the_child = my_list[0][1][1]
the_flag, the_parent = get_parent_list(the_child, my_list)
print (the_flag)
print (the_child)
print (the_parent)
Результат:
True
['3', '4']
[['1', '2'], ['3', '4'], ['5', '6']]
Контрольный пример 2:
the_child = my_list[0][1]
the_flag, the_parent = get_parent_list(the_child, my_list)
print (the_flag)
print (the_child)
print (the_parent)
Результат:
True
[['1', '2'], ['3', '4'], ['5', '6']]
[[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]]
Контрольный пример 3:
the_child = my_list[:]
the_flag, the_parent = get_parent_list(the_child, my_list)
print (the_flag)
print (the_child)
print (the_parent)
Результат:
True
[[[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]], [[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]]]
[[[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]], [[['a', 'b'], ['c', 'd'], ['e', 'f']], [['1', '2'], ['3', '4'], ['5', '6']]]]
Контрольный пример 4:
the_child = my_list[0][1] + ['Non-existent value']
the_flag, the_parent = get_parent_list(the_child, my_list)
print (the_flag)
print (the_child)
print (the_parent)
Результат:
False
[['1', '2'], ['3', '4'], ['5', '6'], 'Non-existent value']
None