TransWikia.com

Make a loop within for loop statement in Python

Stack Overflow Asked by papemoussa on February 8, 2021

I have an initial list named vbn. I am thinking of a function that adds 0 after each 0 in the list.

So that vbn = [1,0,2,3,0,4,5,0] turns into vbn = [1,0,0,2,3,0,0,4,5,0,0].

I used a for loop and .insert() method to do so and obtained the result below.

vbn = [1,0,2,3,0,4,5,0]
s = 0
for i,j in enumerate(vbn[s:]):
    if j == 0:
        vbn.insert(i+1,0)
        s += i+2
print(vbn)

Outputs:

[1, 0, 0, 2, 3, 0, 0, 4, 0, 5, 0]

From what I have understood in my code, the s in my for loop statement is not affected by the iteration at all. s within the loop changes as expected though.

Can someone please explain why is that? And how I can solve the issue.

Thanks in advance!

4 Answers

Although,I couldnt figure out the reason why the s in loop isnt iterating; i though noticed another problem in the code i.e. everytime you set value of s as s+(i+2) eg 0+(1+2), the other time its value is changes as 3+(i+2) in place of 0+(i+2)

Though i have an alternate solution for your question i.e.

vbn = [1,0,2,3,0,4,5,0]
s = 0
arr = []*10 #list to store no. of positions we need to add 0 
for i,j in enumerate(vbn):
  if j == 0:
    arr.append(i)
m=0 #variable to keep count of number of times the list is extended before the current one
for k in arr:
  k+=m #since, when we add zero to the list the current positions of elements changes
  vbn.insert(k+1,0)
  m+=1
print(vbn)

Answered by Devansh Shekhar on February 8, 2021

You can try to create a new list and insert the element (the non-zero number or two zeros) in vbn to the new list.

vbn = [1,0,2,3,0,4,5,0]
newlist = []
for i in vbn: 
    if i == 0: 
        newlist.insert(len(newlist),i)
        newlist.insert(len(newlist),0)
    else: 
        newlist.insert(len(newlist),i)   
print (newlist)

The displayed results is:

[1, 0, 0, 2, 3, 0, 0, 4, 5, 0, 0]

Answered by Yu Xiang on February 8, 2021

As an alternative method if you don't want to iterate multiple values is to create a new array in a loop:

vbn = [1,0,2,3,0,4,5,0]
new = []

for i in vbn:
    if i == 0:
        new += [i, 0]
    else:
        new += [i]
print(new)

Answered by goalie1998 on February 8, 2021

Your problem is that the indexes at which you're inserting are no longer valid after the first insert, because you have increased the size of the list. If you change your code to insert x instead of 0, and print vbn in each iteration of the loop, you can see what is happening:

vbn = [1,0,2,3,0,4,5,0]
s = 0
for i,j in enumerate(vbn[s:]):
    if j == 0:
        vbn.insert(i+1,'x')
        s += i+2
    print(vbn)

Output:

[1, 0, 2, 3, 0, 4, 5, 0]
[1, 0, 'x', 2, 3, 0, 4, 5, 0]
[1, 0, 'x', 2, 3, 0, 4, 5, 0]
[1, 0, 'x', 2, 3, 0, 4, 5, 0]
[1, 0, 'x', 2, 3, 'x', 0, 4, 5, 0]
[1, 0, 'x', 2, 3, 'x', 0, 4, 5, 0]
[1, 0, 'x', 2, 3, 'x', 0, 4, 5, 0]
[1, 0, 'x', 2, 3, 'x', 0, 4, 'x', 5, 0]

As you can see, even the second insertion occurs at the wrong place. You can work around this by adding an offset to the insertion point, and increasing that offset each time you make an insert:

vbn = [1,0,2,3,0,4,5,0]
s = 0
o = 1
for i,j in enumerate(vbn[s:]):
    if j == 0:
        vbn.insert(i+o,'x')
        s += i+2
        o += 1
    print(vbn)

Output (in this case you can see x being inserted in the places you expect):

[1, 0, 2, 3, 0, 4, 5, 0]
[1, 0, 'x', 2, 3, 0, 4, 5, 0]
[1, 0, 'x', 2, 3, 0, 4, 5, 0]
[1, 0, 'x', 2, 3, 0, 4, 5, 0]
[1, 0, 'x', 2, 3, 0, 'x', 4, 5, 0]
[1, 0, 'x', 2, 3, 0, 'x', 4, 5, 0]
[1, 0, 'x', 2, 3, 0, 'x', 4, 5, 0]
[1, 0, 'x', 2, 3, 0, 'x', 4, 5, 0, 'x']

Just change the x back to a 0 to get your desired result. Note that I'm not sure what the s += i+2 code is for; I've left it in on the presumption you use it after the loop.

Answered by Nick on February 8, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP