Delete all the keys except one, but keep all the values in python dictonary

I need to create a dictionary where I am able to split all the select attributes, from table, where condition into different list, but it should have just 1 key. So far I have tried the below-

import pandas
import re
import sqlparse


query = "(select t1.id, t1.feed_id, t2.eff_dt, name, type from table1 t1 INNER JOIN table2 t2 ON ( t1.eff_dt = t2.eff_dt WHERE t2.eff_dt = CAST ( ('2020/12/20') AS DATE) and (t2.name = 'abc' or t2.name='pqr'))"
query2= "SELECT DISTINCT id, feed_id, eff_date FROM"
newInput= input('enter the eff_dt:')
new_where = '("'+newInput+'")'
query1= re.sub("(?<=WHERE t2.eff_dt = CAST \( )(.*)(?= AS DATE)", new_where , query, flags=re.IGNORECASE)
formatted_query = sqlparse.format(query1, reindent=True, keyword_case='lower')
formatted_query1 = sqlparse.format(query2, reindent=True, keyword_case='lower')

a = formatted_query.strip()
a1 = formatted_query1.strip()
newformat = a1+a
newformat.lstrip()
dict_list = newformat.split('\n')

dictOfWords = { i : dict_list[i] for i in range(0, len(dict_list) ) }

newDict = dict(zip(dictOfWords.keys(), [v.strip() if isinstance(v,str) else v for v in dictOfWords.values()]))
newDict

This takes the input from user for date and gives the output as below-

enter the eff_dt:2022/01/20
{0: 'select distinct id,',
 1: 'feed_id,',
 2: 'eff_date',
 3: 'from(',
 4: 'select t1.id,',
 5: 't1.feed_id,',
 6: 't2.eff_dt,',
 7: 'name,',
 8: 'type',
 9: 'from table1 t1',
 10: 'inner join table2 t2 on (t1.eff_dt = t2.eff_dt',
 11: 'where t2.eff_dt = cast (("2022/01/20") as DATE)',
 12: "and (t2.name = 'abc'",
 13: "or t2.name='pqr'))"}

But I need the output as below-


{0: 'select distinct id,','feed_id,','eff_date','from(','select t1.id,','t1.feed_id,','t2.eff_dt,','name,','type','from table1 t1','inner join table2 t2 on (t1.eff_dt = t2.eff_dt','where t2.eff_dt = cast (("2022/01/20") as DATE)',"and (t2.name = 'abc'","or t2.name='pqr'))"}

Is there a way to do it? Remove all the keys except 1, but keep all the values in the dictionary. I would really appreciate some help.



Read more here: https://stackoverflow.com/questions/65710679/delete-all-the-keys-except-one-but-keep-all-the-values-in-python-dictonary

Content Attribution

This content was originally published by Pornima Bansode at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: