I have rather lengthy nested conditionals that are an eyesore. I've tried to turn it into a list comprehension, but failed. I'm not even sure if it's feasible to create such list comprehension. My question is how I can turn the script below into a list comprehension, or perhaps I should leave it as is?
Note: The current script works just fine. The logs coming from the server are really long, so a sample here is not possible.
group_name_list = []
for gn in query_res['hits']['hits']:
    if 'user' not in gn['_source']:
        group_name_list.append('-')
    else:
        gn['_source']['user']
        if 'target' not in gn['_source']['user']:
            group_name_list.append('-')
        else:
            gn['_source']['user']['target']
            if 'group' not in gn['_source']['user']['target']:
                group_name_list.append('-')
            else:
                gn['_source']['user']['target']['group']
                if 'name' not in gn['_source']['user']['target']['group']:
                    group_name_list.append('-')
                else:
                    group_name_list.append(gn['_source']['user']['target']['group']['name'])
Output:
['-', '-', '-', '-', 'Endpoint1', 'PublicWiFi', 'Accounting', 'MobileVPN', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-', '-']
 
     
    