0
Write a program to add item 7000 after 6000 in the following Python List
list1 = [10, 20, [300, 400, [5000, 6000], 500], 30, 40]
12 Answers
+ 2
Amol Bhandekar
You can also use insert function
list1[2][2].insert(2, 7000)
+ 1
You can also use +=
list1[2][2] += [7000]
+ 1
The code Dropped is unnecessary long to solve the problem,and your method is more efficient but as you said for knowledge sake.zen of python(simple is better than complex). Happy coding.
0
Amol Bhandekar
What is your question?
0
Need to add 7000 after 6000 in given list
0
Thanks both of you
Is there any other method or solutions or way without using index.. just for knowledge
0
Amol Bhandekar
b=[10,20,[300,400,[5000,6000],500],30,40]
m=[7000]
reformed=[]
x=0
while x != len(b):
if type(b[x]) == list:
for i in b[x]:
if (type(i) == list) and i[-1]==6000:
i.extend(m)
reformed.extend([b[x]])
#used [b[x]] which is same has list(b[x]) because extend requires a data type list and not any other data type
else:
reformed.extend([b[x]])
x+=1
print(reformed)
0
Thanks š
0
list1[2][2].insert(2,7000)
0
Your functions are not working
0
list1=[10,20,[300,400,[5000,6000],500],30,40]
n=list1[2][2]
n.append(7000)
print(list1)
- 1
Except this is there any way to do
# list1[2][2].append(7000)
# print(list1)