May not be a very elegant solution but you could use locals()
for retrieving the variables and then convert them into a dictionary.
fruit = 'apple'
vegetable = 'potato'
dic = {key:value for key, value in locals().items() if not key.startswith('__')}
This results in {'vegetable': 'potato', 'fruit': 'apple'}
However, I believe a better option would be to pass the variable names and create a dictionary as provided in this answer:
def create_dict(*args):
return dict({i:eval(i) for i in args})
dic = create_dict('fruit', 'vegetable')
Edit: Using eval()
is dangerous. Please refer to this answer for more information.