Write a program to add item 7000 after 6000 in the following Python List
Given:
list1 = [10, 20, [300, 400, [5000, 6000], 500], 30, 40]
Expected output:
[10, 20, [300, 400, [5000, 6000, 7000], 500], 30, 40]
Hint:
The given list is a nested list. Use indexing to locate the specified item, then use the append() method to add a new item after it.
append()
Solution:
Use the append() method
list1 = [10, 20, [300, 400, [5000, 6000], 500], 30, 40] # understand indexing # list1[0] = 10 # list1[1] = 20 # list1[2] = [300, 400, [5000, 6000], 500] # list1[2][2] = [5000, 6000] # list1[2][2][1] = 6000 # solution list1[2][2].append(7000) print(list1)
total answers (1)
start bookmarking useful questions and collections and save it into your own study-lists, login now to start creating your own collections.
Hint:
The given list is a nested list. Use indexing to locate the specified item, then use the
append()
method to add a new item after it.Solution:
Use the
need an explanation for this answer? contact us directly to get an explanation for this answerappend()
method