-1

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.

Darwick
  • 357
  • 3
  • 14
  • 4
    Your question is not clear. If there are no dicts with the key type as "a1", nothing will be printed... – Tomerikoo Mar 14 '23 at 14:31
  • 1
    Also is `OrderedDict` really relevant to the question? – Tomerikoo Mar 14 '23 at 14:40
  • 1
    [Group/Count list of dictionaries based on value](https://stackoverflow.com/q/15815976/6045800) – Tomerikoo Mar 14 '23 at 15:10
  • You are right, sorry for missleading, I modified my question then I think it is clear now. – Darwick Mar 14 '23 at 15:56
  • 1
    I don't understand. You get the error because you treat your input (`d`) as a list of dicts, but it is actually a dict. Then you say you want to check *"if the key "type" has more then 0 value with "a1""*. What you really want to check is whether `d` is a list or a dict? – Tomerikoo Mar 14 '23 at 15:59
  • Exactly. Now I realized the API answers, this would be the best. – Darwick Mar 14 '23 at 16:10
  • [What's the canonical way to check for type in Python?](https://stackoverflow.com/q/152580/6045800) – Tomerikoo Mar 14 '23 at 16:12

3 Answers3

1

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.

Kurt
  • 1,708
  • 1
  • 3
  • 11
0

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"]
chepner
  • 497,756
  • 71
  • 530
  • 681
-1

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"])
John Gordon
  • 29,573
  • 7
  • 33
  • 58
  • That's not what is asked for. The question is not super clear, but it seems the key `type` is always there. OP is asking about when it has `a1` as the value – Tomerikoo Mar 14 '23 at 15:06