I have multiple ordered dict (in this example 3, but can be more) as a result from an API, with same key value pairs and I test for the same key, then I print out another key's value:
from collections import OrderedDict
d = [OrderedDict([('type', 'a1'), ('rel', 'asd1')]),
OrderedDict([('type', 'a1'), ('rel', 'fdfd')]),
OrderedDict([('type', 'b1'), ('rel', 'dfdff')])]
for item in d:
if item["type"] == "a1":
print(item["rel"])
This works fine, but it could happen that the return from API can be different, because there is only 1 result. Without the [ at the begining and ] at the end, like this:
d = OrderedDict([('type', 'b1'), ('rel', 'dfdff')])
In this case, I will receive this error:
if item["type"] == "a1":
TypeError: string indices must be integers
So I would like to test also (maybe in the same if it is possible) if the key "type" has more then 0 value with "a1" then print the items or to test the OrderedDict have the square brackets at the begining or at the end.