0

How to add value to a list without insert () operators?

https://code.sololearn.com/cOeLjXd5P3KI/?ref=app

19th Jan 2022, 5:40 PM
Thile Dorje Lama
Thile Dorje Lama - avatar
7 odpowiedzi
+ 1
Ahh, then it looks like you may actually need a deepcopy instead of a shallow copy. Something like this might be more what you're after. from copy import deepcopy a = [[1,2,3,4,5], [6,7,8,9,10]] def creat_six_digits(a, value_given): b = deepcopy(a) c = b[0][-1]; e, f = divmod(c * value_given, 5) b[0][-1] = e b[0] += [f] return b
22nd Jan 2022, 6:41 PM
ChaoticDawg
ChaoticDawg - avatar
+ 3
You can append a list to another list using the += operator. Like; a = [[1,2,3,5], [9,8,7,6]] b = 4 a[1] += [b] # without insert() use print(a) # result should be. # [[1,2,3,5], [9,8,7,6,4]] To insert into the list you can use slicing of the list to the location in the list you wish to insert at; a = [[1,2,3,5], [9,8,7,6]] b = 4 a[0][:3] += [b] # without insert() use print(a) # result should be. # [[1,2,3,4,5], [9,8,7,6]]
19th Jan 2022, 6:01 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
Then you can just make a shallow copy of a and operate on that for your new variable. a = [[1,2,3,5], [9,8,7,6]] b = 4 c = a[:] # make copy of a so a is unchanged c[0][:3] += [b] # without insert() use print(c) # result should be. # [[1,2,3,4,5], [9,8,7,6]]
19th Jan 2022, 6:37 PM
ChaoticDawg
ChaoticDawg - avatar
+ 1
Thiley Dorje Lama Slice assignment is all I can think of, as pointed out by ChaoticDawg
19th Jan 2022, 8:36 PM
Œ ㅤ
Œ ㅤ - avatar
0
I need result in new variable.
19th Jan 2022, 6:31 PM
Thile Dorje Lama
Thile Dorje Lama - avatar
22nd Jan 2022, 11:48 AM
Thile Dorje Lama
Thile Dorje Lama - avatar
0
This is the problem I couldn't solve I need only six digits But using operators digits keep adding in all applications
22nd Jan 2022, 11:49 AM
Thile Dorje Lama
Thile Dorje Lama - avatar