Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Change value every nth iteration python

How can I change a value inside a loop every 10th iteration?

Say I have a list with 30 properties

myList = {"One", "Two", "Three", "Four"........,"Thirty" } #trimmed
listLength = len(myList) #30
listRange = range(0, listLength) # 0-30

for i in listRange
    
    x = ???
    ...
    
    ops.mesh.add(size=2, location=(x)) # change value of x by 10 every 10 value
    

I would like to change the value of x by a factor of 10 every 10th time.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Desired outcome

first 10 values x = 10
second 10 values x = 20
third 10 values x = 30

**

>Solution :

Modulo is a nice way of doing that:

if i % 10 == 0:
  x+=10

Edit: you should probably initialize x outside the loop.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading