I want to insert an item into a list inside a list. I'm wondering if someone can show me.
list5 = [[], [(1,2,3,4), 2, 5]]
print("1. list5", list5)
list5.insert(0, (2,5,6,8))
print("2. list5", list5)
Output:
1. list5 [[], [(1, 2, 3, 4), 2, 5]]
2. list5 [(2, 5, 6, 8), [], [(1, 2, 3, 4), 2, 5]]
I want:
2. list5 [[(2, 5, 6, 8)], [(1, 2, 3, 4), 2, 5]]
A dictionary unfortunately won't work.
The problem is you are trying to insert as the first element of the list,
list5
which is incorrect. You have to access the first element of the list and insert it to that list. This can be done using the following code