[Answer]-How to combine array lists gets in python?

1👍

Simply substitute the references for the variable names in the last expression:

education = result[0]['education'][0]['school']['name']

Not certain that this is more readable though.

0👍

Martijn is right – I’m not sure about the readability – if you find yourself doing this often enough, then I would be tempted to use a helper function… It makes things clearer.

def get_education(edu, main_idx=0, edu_idx=0):
    "Get the name of the school where education was received"
    return edu[main_idx]['education'][edu_idx]['school']['name']

Then just use:

education = get_education(result)

0👍

Keep it on 3 lines. It’s quite readable and really doesn’t take longer to run.

You’ll thank me when you are trying to track down an KeyError or IndexError from that block.

Maybe you can improve the variable names. Here’s my suggestion

education = result[0]['education']
school = education[0]['school']
school_name = school['name']

Leave a comment