-1

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

3 Answers3

1

You are seeing what you are seeing because Objects is a dictionary. If Objects were a list of lists, then your loop would work. If you want it as a dictionary,

for tag, object_list in Objects.items():
    for Object in object_list:
        print(Object)
cup
  • 7,589
  • 4
  • 19
  • 42
0

Try the following iteration:

for Type in Objects:
    for Object in Objects[Type]: 
        print(Object)
0

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)
Barmar
  • 741,623
  • 53
  • 500
  • 612