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)
- [Answer]-ProgrammingError when uploading image
- [Answer]-Django User Authentification
- [Answer]-Why isn't my Django template properly calculating the length of my collection?
- [Answer]-SWIG: python module crashes only when it is deployed to Apache
- [Answer]-Django : GAE : non rel : maximum recursion depth exceeded in cmp
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']
Source:stackexchange.com