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

How do I return only one element from at a time and and int from range in python for loop

So I tried using this block of code but what it does is it puts the items from resources index 0 and 1 into both variables on the first iteration. I want a way to where resources will map to alph and range will map to i. Is this possible in python?

resources = ["a", "b", "c"]
for alph, i in resources, range(len(resources)):
     print(alph)
     if i == 2:
         print(2)

I’m basically looking for something similar to golang’s range where you would type

resources := ["a", "b", "c"]
for incrementer, alph := range resources {
     fmt.Print(alph)
     if i == 2 {
         fmt.Print(2)
     }
}

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

>Solution :

This should work

resources = ["a", "b", "c"]
for key, value in enumerate(resources):
     if value == "a":
         print(str(key) + "-" + value)
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