create dictionary from list of variables
Clash Royale CLAN TAG#URR8PPP
create dictionary from list of variables
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 name are the keys and their value is the variable value
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 anyway to do so?
5 Answers
5
You cannot do this.
Your function could be defined as:
def create_dict_from_variables(first_name, second_name):
return something_from(first_name, second_name)
and you can call this function:
create_dict_from_variables('abcd', 'efgh')
These two arguments 'abcd'
and 'efgh'
are not named variables.
'abcd'
'efgh'
You can't do it without writing at least the variable names, but a shorthand can be written like this:
>>> foo = 1
>>> bar = 2
>>> d = dict(((k, eval(k)) for k in ('foo', 'bar')))
>>> d
'foo': 1, 'bar': 2
or as a function:
def createDict(*args):
return dict(((k, eval(k)) for k in args))
>>> createDict('foo','bar')
'foo': 1, 'bar': 2
you can also use globals()
instead of eval()
:
globals()
eval()
>>> dict(((k, globals()[k]) for k in ('foo', 'bar')))
You can use locals
, but I would recommend against it. Do it explicitly.
locals
>>> import this
[...]
Explicit is better than implicit.
[...]
Your code will generally be better, more predictable, less prone to breaking and more comprehensible if you do it explicitly.
@user1087310: well then, you've thought about the only possibility that there is. Now that you have discarded that, know that there is no way of doing it; do it explicitly and enjoy!
– Chris Morgan
Feb 29 '12 at 8:06
It has been a long time since i tackled something that i cant do in python. I'm writing few functions that need to return bulk of parmeters for other functions. the returned dictionary is almost identical to the variables names, so i have high chance to make typo when i write the key explicitly. But my grandfather told me once that life is not easy :)
– user1087310
Feb 29 '12 at 8:17
No, there isn't, because Python functions don't get information about the variables used to call the function. Also, imagine doing something like this:
create_dict_from_variables(first_name[:-3] + "moo", last_name[::2])
The function will have no way of knowing the expressions used to create the parameters.
In principle this is possible by passing variable names to function create_dict
and then reaching to a caller stack frame from within create_dict
function using module inspect
.
create_dict
create_dict
inspect
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Thought about locals, but i dont want to pass around many temp variables
– user1087310
Feb 29 '12 at 8:00