I\'m looking for a way to create dictionary without writing the key explicitly I want to create function that gets number variables, and create dictionary where the variables na
In fact, there is a way:
from varname import nameof
def foo(first_name,second_name):
return {nameof(first_name):first_name, nameof(second_name):second_name}
first_name = "daniel"
second_name = "daniel"
print (foo(first_name,second_name))
Output:
{'first_name': 'daniel', 'second_name': 'daniel'}
You can get the python-varname
package below:
https://github.com/pwwang/python-varname
Basic use:
from varname import nameof
s = 'Hey!'
print (nameof(s))
Output:
s