'insert list in multilayer list in python

I have a code like:

a = ['layer 1', 2, ['layer 2', 22, ['layer 3', 222]]]

and I want using insert method add the following list ['layer 4', 2222, 3333] into index 1st of layer 3rd list

what I want is:

['layer 1', 2, ['layer 2', 22, ['layer 3', ['layer 4', 2222, 3333], 222]]]


Solution 1:[1]

Fairly straightforward indexing leads to this:

a = ['layer 1', 2, ['layer 2', 22, ['layer 3', 222]]]
b = ['layer 4', 2222, 3333]
a[-1][-1].insert(1, b)
print(a)

Output:

['layer 1', 2, ['layer 2', 22, ['layer 3', ['layer 4', 2222, 3333], 222]]]

Solution 2:[2]

You have to insert it into the inner list. The inner list is accessed via index:

a = ['layer 1', 2, ['layer 2', 22, ['layer 3', 222]]]
b = ['layer 4', 2222, 3333]
a[2][2].insert(1, b)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Albert Winestein
Solution 2