Theo Theo - 3 months ago 10
Python Question

python list as index for a nested list

I have the following two lists:

l1 = [2, 3, 2]
l2 = [0, [0, 1], [0, 1, 0, [0, 1, [0, 0]]]]


How can I use the first list as a tree index, in order to append an item to the second list, like doing

l2[2][3][2].append(0)

Answer

Do this only if you know what you're doing:

l1 = [2, 3, 2]
l2 = [0, [0, 1], [0, 1, 0, [0, 1, [0, 0]]]]

str1=repr(l1)
str1=str1.replace(', ','][') # str1=[2][3][2]
eval(repr(l2)+str1) # or var=exec(repr(l2)+str1)