0

This question is a bit of a convoluted brain-twister, I'm afraid. I'm writing a function test on an api, that when I query it, returns a bunch of json with embedded lists. Here is a significant fragment of what that looks like (with all the data anonymized for this question):

[{u'account': {u'account_name': u'Autotest Account',
               u'account_uid': u'000000000'},
  u'address': {u'city': u'AutoTest City',
               u'country': u'United States',
               u'postal_code': u'10019',
               u'province': None,
               u'state': u'IL',
               u'street': [u'12 Auto Road']},
  u'children': [{u'institution_name': u'Autotest Bottom Institution 1',
                 u'institution_type': 1,
                 u'institution_uid': u'111111111'},
                {u'institution_name': u'Autotest Bottom Institution 2',
                 u'institution_type': 1,
                 u'institution_uid': u'222222222'},
                {u'institution_name': u'Autotest Bottom Institution 3',
                 u'institution_type': 1,
                 u'institution_uid': u'333333333'},
                {u'institution_name': u'Autotest Bottom Institution 4',
                 u'institution_type': 1,
                 u'institution_uid': u'444444444'},
                {u'institution_name': u'Autotest Bottom Institution 5',
                 u'institution_type': 1,
                 u'institution_uid': u'555555555'},
                {u'institution_name': u'Autotest Bottom Institution 6',
                 u'institution_type': 1,
                 u'institution_uid': u'666666666'},
                {u'institution_name': u'Autotest Bottom Institution 7',
                 u'institution_type': 1,
                 u'institution_uid': u'777777777'},
                {u'institution_name': u'Autotest Bottom Institution 8',
                 u'institution_type': 1,
                 u'institution_uid': u'888888888'}],
  u'institution_name': u'Autotest Middle Institution 1',
  u'institution_type': 2,
  u'institution_uid': u'000000001',
  u'parent': {u'institution_name': u'Autotest Top Institution',
              u'institution_type': 3,
              u'institution_uid': u'000000099'},
  u'school_year': 2011},
 {u'account': {u'account_name': u'Autotest Account',
               u'account_uid': u'000000000'},
  u'address': {u'city': u'Bacon City',
               u'country': u'United States',
               u'postal_code': u'10018',
               u'province': None,
               u'state': u'IL',
               u'street': [u'15 Wonder Road']},
  u'children': [],
  u'institution_name': u'Autotest Bottom Institution 1',
  u'institution_type': 1,
  u'institution_uid': u'111111111',
  u'parent': {u'institution_name': u'Autotest Middle Institution 1',
              u'institution_type': 2,
              u'institution_uid': u'000000001'},
  u'school_year': 2011}]

What I'm trying to accomplish, is to extract all of the "Bottom Institution" names from the JSON, and put them into a list that I can then compare to a list that's already in my test fixture data. It should look something like this:

['Autotest Bottom Institution 1','Autotest Bottom Institution 2','Autotest Bottom Institution 3','Autotest Bottom Institution 4','Autotest Bottom Institution 5','Autotest Bottom Institution 6','Autotest Bottom Institution 7','Autotest Bottom Institution 8']

I'm able to extract them one-at-a-time or via iteration, after loading the data into "inst_array", like this:

>>> print inst_array[0]['children'][0]['institution_name']
Autotest Bottom Institution 1
>>> print inst_array[0]['children'][1]['institution_name']
Autotest Bottom Institution 2
>>> print inst_array[0]['children'][2]['institution_name']
Autotest Bottom Institution 3

But here's the kicker: I want to be able to do this without iteration (or, with as little iteration as possible), and because of the umpteen layers of nesting in this, It's got me puzzled.

Any thoughts?

5 Answers 5

5

Shouldn't something like this work?

names = [child['institution_name'] for child in inst_array[0]['children']]
Sign up to request clarification or add additional context in comments.

1 Comment

you sir, are a genius. Yeah, that's exactly what I wanted. For some reason, when I tried this, I couldn't get "child['institution_name'] to work. I'll have to go back and look at my notes...
1

I guess a list comprehension is still a form of iteration, but at least concise:

your_list = [elem['institution_name'] for elem in inst_array[0]['children']]

Comments

0

Not sure about what you mean by 'without iteration'. Whatever you do here has to involve iterating through the lists, by definition.

Anyway, here's an attempt:

institutions = set()
for account in data:
    for child in account['children']:
        institutions.add(child['institution_name'])    

1 Comment

sorry about the confused language - you're quite right. I was looking for a list comprehension technique, which technically still is iteration, but in a short-hand form. :/
0

I'm not sure what do you mean exactly with "without iteration", but here is something with implied iteration that could work:

 [x['institution_name'] for x in inst_array[0]['children'] if x['institution_name']]

Comments

0

Something like:

[ x['institution_name'] for x in inst_array[0]['children'] ]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.