Below is an example to elaborate the error and for an better understanding.
Eg:
shops = [{
"name": "Anand Motors",
"cars": [
{
"name":"Ferrari",
"Price": 25000
},
{
"name": "Benz",
"price": 15000
}
]}
]
The above dictionary is referred as list, when to try to access the dictionary data using string.
name = "Anand Motors"
print(shops[name])
The above code gives error.
Solution or fix:
name = "Anand Motors"
print(next(shop for shop in shops if shop["name"] == name))
The above code will fix this error.
No comments:
Post a Comment