Home > other >  I do have a list of dictionaries and I am interested in getting values of only select key
I do have a list of dictionaries and I am interested in getting values of only select key

Time:10-05

I have used this code on my dataframe of sql queries to obtain list of dictionaries that has select, where, join columns:

def augment(x):
    try:
        return Parser(x).columns_dict
    except:
        pass

df_1_joins_where= df_1['SQL_TEXT'].apply(lambda x: augment(x))

I get a list of dictionary like this:

 {'select': ['all_cons_columns.column_name'], '...
1              {'select': ['free_mb'], 'where': ['name']}
2       {'select': ['all_cons_columns.column_name'], '...
3       {'select': ['all_cons_columns.column_name'], '...
4       {'select': ['AA.BB.CC_...
                              ...                        
4995    {'select': ['AA.BB.CC...
4996    {'select': ['AA.BB.CC_...
4997    {'select': ['AA.BB.CC_...
4998    {'select': ['AA.BB.CC_...
4999                                    {'select': ['*']} 

Single dictionary in my list could look like this:

{'select': ['Names'], 'where': ['point']}

or could have just select key or:

{'select': ['Table.ColumnA']]}

or it could have all three (select, where, join) keys as well

I want to extract all the values paired with where key, how could I do this?

Thanks

CodePudding user response:

This should give you a list of all values across all dicts that have the "where" key:

dict_list = df_1_joins_where= df_1['SQL_TEXT'].apply(lambda x: augment(x))

all_where_values = []
for d in dict_list:
    if 'where' in d:
         all_where_values  = [d["where"]]

Note that, since in general the value of keys "where" is a list, all_where_values with be a list of lists.

  • Related