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 Can I Use Nested Loops on Pyhton

I saw an example about using nested loop in pyhton.

students = [['Igor', 'Sokolov'], ['Riko', 'Miyazaki'], ['Tuva', 'Johansen']]
for student in students:
   for name in student:
       print(name)
   print()

I want to use same thing below:

def id_validator(verified_ids,feedback_ids):
    for ids in id_validator:
        for x in ids:
            print(x)
    
    
    
id_validator([5, 8], [5,8,30,50])

I expected to see
5
8
5
8
30
50
but I got a Type Error:’function’ object is not iterable

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 :

Your problem is that you are using the same name id_validator for both the function and the loop variable! to fix this, you need to use a different variable name for the loop!

so check this out:

def id_validator(verified_ids, feedback_ids):
    for ids in [verified_ids, feedback_ids]:
        for x in ids:
            print(x)

id_validator([5, 8], [5, 8, 30, 50])

output will be 5 8 5 8 30 50

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