+ 1

how to get the result of [1,1].insert(1,3)([1,3,1]) without using b=[1,1]b.insert(1,3) ?

because the latter one b will keep changing in a loop where i can insert many elements in b ,i don't want that,i want b to be [1,1]all the time during a loop where i can insert other element to it like a pattern.Anyone could help with this?

19th Oct 2017, 9:40 AM
lwluowei
7 Answers
0
## else u can do this....if u wanna incorporate "insert" in your code in each step anyway a=[1, 1, 1, 1] temp=a.copy() for i in range(5): a.insert(2,i) print(a) a=[i for i in temp]
20th Oct 2017, 7:27 AM
sayan chandra
sayan chandra - avatar
+ 2
You mean [1,1].insert(1,3) doesn't work?
19th Oct 2017, 9:51 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
0
we don't have a variant to store the result [1,3,1]using [1,1].insert()
19th Oct 2017, 9:55 AM
lwluowei
0
Imaging this: def add_pattern(n): a=[1,1,1,1] for i in range(n): a.insert(2,i) print a if n=3,print [1,1,0,1,1],[1,1,1,0,1,1] etc.We lose the original pattern [1,1,1,1] which can be replaced by many other patterns. but what i want is [1,1,0,1,1],[1,1,1,1,1],[1,1,2,1,1]etc.Maybe it is just not the way to do that.Many other way to do that,but i just want to try it anyway. thanks for your answer.still very helpful😀
20th Oct 2017, 6:06 AM
lwluowei
0
yep, We just need to transfer the changed a into the unchanged a(pattern) at the end of the loop, although it seem not to be the best way.Thank 😀you so much
20th Oct 2017, 9:57 AM
lwluowei
- 1
python dont work like that.... in c ; java u need to declair a var furst then initialise... but in python... any initialisation is itsekf declairisation.. like that... when u are doing a=[1, 1] a.insert(1, 3) it makes a [1,3,1] and then returns a but when u directly do [1, 1].insert(1, 3) ## think it off like that## python virtually creats a var copies [1, 1] in it changes that var into [1, 3, 1] returns the list in var as[1, 3, 1] ##destroying the var
20th Oct 2017, 5:07 AM
sayan chandra
sayan chandra - avatar
- 1
the thing is what u wanna do has nothing to do with insert except 1st move--------<> a=[1, 1, 1, 1] a.insert(2,0) print(a) for i in range(1,5): a[2]=i print(a)
20th Oct 2017, 7:23 AM
sayan chandra
sayan chandra - avatar