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 to iterate over a list with named values python

I want to be able to iterate over a list with named lists inside

I tried doing this:


Objects = {

    "Enemies":[

        "Enemy1",

        "Enemy2",

        "Enemy3"

    ],

    "Walls":[

        "Wall1",

        "Wall2",

        "Wall3"

    ]

}

for Type in Objects:

    for Object in Type:

        print(Object)

But this doesn’t work and prints the letters of the Types under each other

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 :

for Type in Objects loops over the keys of the dictionary. You want to loop over the values.

for v in Objects.values():
    for name in v:
        print(name)
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