Q:

Write a python program to add item 7000 after 6000 in the following Python List

belongs to collection: Python List Exercises

0

Add new item to list after a specified item

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]

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 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)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Extend nested list by adding the sublist using pyt... >>
<< Remove empty strings from the list of strings usin...