smack
smack

Reputation: 950

Filter Nested Dict With Key and Get Item in the dict

I need to search through the list dict with the key 'name' and return the dict of the first match. E.g Search for 'Sales' using 'name' and return the the dict items.

second_step = [{'node': {'id': 'QWNjb3VudDo5NDI1MjA4NDI1NDAxMDgwMTQ7QnVzaW5lc3M6MGRmZDM5ODAtNzQ3OS00ZGQ4LTg5NWYtMzU4ZWNiNDNmMTI2', 'name': 'Commodity Credit Loans', 'description': 'Income received from a Commodity Credit Corporation as part of a farm price and income support commodity program.', 'displayId': None, 'type': {'name': 'Income', 'value': 'INCOME'}, 'subtype': {'name': 'Income', 'value': 'INCOME'}, 'normalBalanceType': 'CREDIT', 'isArchived': False}}, {'node': {'id': 'QWNjb3VudDo5NDI1MjA4NDI2MjM5OTQwOTg7QnVzaW5lc3M6MGRmZDM5ODAtNzQ3OS00ZGQ4LTg5NWYtMzU4ZWNiNDNmMTI2', 'name': 'Sales', 'description': 'Payments from your customers for products and services that your business sold.', 'displayId': None, 'type': {'name': 'Income', 'value': 'INCOME'}, 'subtype': {'name': 'Income', 'value': 'INCOME'}, 'normalBalanceType': 'CREDIT', 'isArchived': False}}, {'node': {'id': 'QWNjb3VudDo5NDI1MjA4NDI1ODIwNTEwNTY7QnVzaW5lc3M6MGRmZDM5ODAtNzQ3OS00ZGQ4LTg5NWYtMzU4ZWNiNDNmMTI2', 'name': 'Services', 'description': 'Income received from professional services rendered to your customers.', 'displayId': None, 'type': {'name': 'Income', 'value': 'INCOME'}, 'subtype': {'name': 'Income', 'value': 'INCOME'}, 'normalBalanceType': 'CREDIT', 'isArchived': False}}]

I did this:

next((item for item in second_step if item["name"] == "Sales"), None)

I got this error:

Traceback (most recent call last):
  File "<pyshell#429>", line 1, in <module>
    next((item for item in second_step if item["name"] == "Sales"), None)
  File "<pyshell#429>", line 1, in <genexpr>
    next((item for item in second_step if item["name"] == "Sales"), None)
KeyError: 'name'

I want the below to return as result.

{'id': 'QWNjb3VudDo5NDI1MjA4NDI2MjM5OTQwOTg7QnVzaW5lc3M6MGRmZDM5ODAtNzQ3OS00ZGQ4LTg5NWYtMzU4ZWNiNDNmMTI2', 'name': 'Sales', 'description': 'Payments from your customers for products and services that your business sold.', 'displayId': None, 'type': {'name': 'Income', 'value': 'INCOME'}, 'subtype': {'name': 'Income', 'value': 'INCOME'}, 'normalBalanceType': 'CREDIT', 'isArchived': False}

What am I missing?

Upvotes: 0

Views: 49

Answers (3)

Dcoder14
Dcoder14

Reputation: 1939

The problem was you were checking item["name"] but before that there is a another key named "node". For this reason you were getting error.

As if you return only item inside loop as below,

next((item for item in second_step if item["node"]["name"] == "Sales"), None)

you will get

{'node': {'id': 'QWNjb3VudDo5NDI1MjA4NDI2MjM5OTQwOTg7QnVzaW5lc3M6MGRmZDM5ODAtNzQ3OS00ZGQ4LTg5NWYtMzU4ZWNiNDNmMTI2', 'name': 'Sales', 'description': 'Payments from your customers for products and services that your business sold.', 'displayId': None, 'type': {'name': 'Income', 'value': 'INCOME'}, 'subtype': {'name': 'Income', 'value': 'INCOME'}, 'normalBalanceType': 'CREDIT', 'isArchived': False}}

So to get your required answer you have to do as below:

next((item["node"] for item in second_step if item["node"]["name"] == "Sales"), None)

which will give you output as

{'id': 'QWNjb3VudDo5NDI1MjA4NDI2MjM5OTQwOTg7QnVzaW5lc3M6MGRmZDM5ODAtNzQ3OS00ZGQ4LTg5NWYtMzU4ZWNiNDNmMTI2', 'name': 'Sales', 'description': 'Payments from your customers for products and services that your business sold.', 'displayId': None, 'type': {'name': 'Income', 'value': 'INCOME'}, 'subtype': {'name': 'Income', 'value': 'INCOME'}, 'normalBalanceType': 'CREDIT', 'isArchived': False}

Upvotes: 1

Pato Navarro
Pato Navarro

Reputation: 300

you have first to access to the key "node"

You may do it this way:

next((item for item in second_step if item["node"]["name"] == "Sales"), None)

Upvotes: 1

iamsan
iamsan

Reputation: 111

next((item for item in second_step if item["node"]["name"] == "Sales"), None)

Upvotes: 1

Related Questions