Python count same keys in ordered dict
Question:
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.
Answers:
You can simply keep a flag that remembers if item["type"] == "a1"
was ever true.
has_a1 = False
for item in d:
if item["type"] == "a1":
has_a1 = True
print(item["rel"])
If you want the actual count, use an integer variable instead of a Boolean.
a1_count = 0
for item in d:
if item["type"] == "a1":
a1_count += 1
print(item["rel"])
If you don’t mind using more memory (and maybe there’s a reason to do this anyway), keep a list of item["rel"]
values instead of printing them on demand, then check the length of the list later.
a1_values = [item["rel"] for item in d if item["type"] == "a1"]
If you want to check that a key is present in a dictionary, without caring what specific value it has, use this:
if key in mydict:
So in your case, it would look like this:
if "type" in item:
print(item["rel"])
The "square brackets" represent a list object. You simply need to check if d
is a list
or an OrderedDict
and behave accordingly.
if isinstance(d, list):
for item in d:
if item["type"] == "a1":
print(item["rel"])
elif isinstance(d, OrderedDict):
if d["type"] == "a1":
print(d["rel"])
The TypeError
happens because when d is an OrderedDict rather than a list of OrderedDicts, the for
loop iterates over the items in the OrderedDict, and those are strings.
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.
You can simply keep a flag that remembers if item["type"] == "a1"
was ever true.
has_a1 = False
for item in d:
if item["type"] == "a1":
has_a1 = True
print(item["rel"])
If you want the actual count, use an integer variable instead of a Boolean.
a1_count = 0
for item in d:
if item["type"] == "a1":
a1_count += 1
print(item["rel"])
If you don’t mind using more memory (and maybe there’s a reason to do this anyway), keep a list of item["rel"]
values instead of printing them on demand, then check the length of the list later.
a1_values = [item["rel"] for item in d if item["type"] == "a1"]
If you want to check that a key is present in a dictionary, without caring what specific value it has, use this:
if key in mydict:
So in your case, it would look like this:
if "type" in item:
print(item["rel"])
The "square brackets" represent a list object. You simply need to check if d
is a list
or an OrderedDict
and behave accordingly.
if isinstance(d, list):
for item in d:
if item["type"] == "a1":
print(item["rel"])
elif isinstance(d, OrderedDict):
if d["type"] == "a1":
print(d["rel"])
The TypeError
happens because when d is an OrderedDict rather than a list of OrderedDicts, the for
loop iterates over the items in the OrderedDict, and those are strings.