0
How to add value to a list without insert () operators?
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
+ 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]]
+ 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]]
+ 1
Thiley Dorje Lama Slice assignment is all I can think of, as pointed out by ChaoticDawg
0
I need result in new variable.
0
This is the problem I couldn't solve
I need only six digits
But using operators digits keep adding in all applications