I’m looking for a way to create a dictionary without writing the key explicitly.
I want to create a function that gets the number of variables and creates a dictionary where the variable names are the keys and their values are the variable values.
Instead of writing the following function:
def foo():
first_name = "daniel"
second_name = "daniel"
id = 1
return {'first_name':first_name, 'second_name':second_name}
I want to get the same result with function :
create_dict_from_variables(first_name, second_name)
Is there any way to do so?
You cannot do this.
Your function could be defined as:
and you can call this function:
These two arguments
'abcd'and'efgh'are not named variables.