[Python] How to loop in array of dictionaries in a list

Discussão em 'Python' iniciado por Stack, Agosto 6, 2025.

  1. Stack

    Stack Membro Participativo

    Trying to loop in an array of dictionary in list I am getting this error message:


    TypeError: list indices must be integers or slices, not dict
    ​

    What am I doing wrong here?

    cars= [
    {
    "name": "Ford",
    "type": "SHORT",
    "coded": {
    "1": "Escape",
    "2": "Fiesta",
    "999": "Gumbo"
    }
    },
    {
    "name": "Honda",
    "type": "SHORT",
    "coded": {
    "1": "Civic",
    "2": "CRV",
    "VCR": "Accord"
    }
    }
    ]

    for i in cars:
    for x, y in cars["coded"].items():
    print(x, y)


    I am getting this error:

    Traceback (most recent call last):
    File "<string>", line 23, in <module>
    TypeError: list indices must be integers or slices, not dict

    Continue reading...

Compartilhe esta Página