Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting list values with afilter #141

Open
sehmaschine opened this issue Nov 11, 2020 · 0 comments
Open

Getting list values with afilter #141

sehmaschine opened this issue Nov 11, 2020 · 0 comments
Assignees
Labels

Comments

@sehmaschine
Copy link

I might be missing something, but why does afilter not get dict/list values? Any specific reason for that?

data = [
  {
    "choices": [
      "Charity",
      "Cooperative",
      "Corporation",
      "Educational institution",
      "Public entity",
      "International organisation"
    ]
  },
  {
    "items": [
      {
        "choices": "country"
      }
    ],
  }
]

def afilter(x):
    print x
    return True

res = dpath.util.search(data, "**/choices", yielded=True, afilter=afilter)

Running this results in ...

country
('1/items/0/choices', 'country')

Or to phrase it differently: how can I search for elements which include "Charity"? My current solution looks like this:

res = dpath.util.search(data, "**/choices", yielded=True)

for item in res:
    val = dpath.util.get(data, item[0])
    if "Charity" in val:
        print item[0]

But that would be much simpler if afilter also gets the list of the first element.

@moomoohk moomoohk self-assigned this Feb 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants