0

I am trying to increase a variable in a for loop dynamicly .

first iteration: i=i+4

second iteration: i= i+5

third iteration i= i+6 ....

Trying:

first iteration

    for i in d:         # d has lenght 2 
      i=i+4 # i = 17 
      sheet.insert_rows(idx=i, amount=1)
      

after the first iteration the value of i is now 23.

now do this:

second interation

      for i in d:         # d has lenght 2 
      i=i+5 # i = 28
      sheet.insert_rows(idx=i, amount=1)




      

   
2
  • for i in d gives i the values contained in d. Commented Jun 16, 2021 at 13:51
  • what do you mean with manage i manually Commented Jun 16, 2021 at 13:51

2 Answers 2

2

You can do it using enumerate,

from docs,

Return an enumerate object. iterable must be a sequence, an iterator, or some other object which supports iteration. The next() method of the iterator returned by enumerate() returns a tuple containing a count (from start which defaults to 0) and the values obtained from iterating over iterable.

>>> for index, i in enumerate([3, 4], start=4):
...     print(index, i)
...
4 3
5 4
>>>
Sign up to request clarification or add additional context in comments.

Comments

0

You don't need the i=i+4 operation, try this out.

x = 4
d = [1, 2]  # i assume you have list like this
for i in d:
    x += 1
    sheet.insert_rows(idx=x, amount=1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.