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]

21st Feb 2022, 6:53 AM
Amol Bhandekar
Amol Bhandekar - avatar
12 Answers
+ 2
Amol Bhandekar You can also use insert function list1[2][2].insert(2, 7000)
21st Feb 2022, 7:25 AM
A͢J
A͢J - avatar
+ 1
You can also use += list1[2][2] += [7000]
21st Feb 2022, 9:15 AM
ChaoticDawg
ChaoticDawg - avatar
+ 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.
21st Feb 2022, 1:06 PM
Afolabi Mafolasere Tobias
0
Amol Bhandekar What is your question?
21st Feb 2022, 7:19 AM
A͢J
A͢J - avatar
0
Need to add 7000 after 6000 in given list
21st Feb 2022, 7:21 AM
Amol Bhandekar
Amol Bhandekar - avatar
0
Thanks both of you Is there any other method or solutions or way without using index.. just for knowledge
21st Feb 2022, 10:31 AM
Amol Bhandekar
Amol Bhandekar - avatar
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)
21st Feb 2022, 1:03 PM
Afolabi Mafolasere Tobias
0
Thanks 🙂
21st Feb 2022, 2:52 PM
Amol Bhandekar
Amol Bhandekar - avatar
0
list1[2][2].insert(2,7000)
12th Sep 2022, 9:10 PM
Amany ElBana
Amany ElBana - avatar
0
Your functions are not working
7th Dec 2022, 2:16 PM
Esiana Emmanuel
Esiana Emmanuel - avatar
0
list1=[10,20,[300,400,[5000,6000],500],30,40] n=list1[2][2] n.append(7000) print(list1)
6th Aug 2024, 7:28 AM
Shweta Datey
Shweta Datey - avatar
- 1
Except this is there any way to do # list1[2][2].append(7000) # print(list1)
21st Feb 2022, 6:58 AM
Amol Bhandekar
Amol Bhandekar - avatar